Why Does Linq Lambda Work One Way But Not The Other
Dec 7, 2011
I am learning MVC3 and transitioning to VB.NET from C# at the same time (Fun, I know). I am running through the MvcMusicStore sample for learning MVC3. All the samples are in C# so I am converting them to VB. I came across one piece of code that I was unable to directly convert and had to change slightly to get it to work.
why one way works and the other doesn't.
This does not work:
Dim albums = New List(Of Album)() From { _
New Album() With { _
.Title = "A Copland Celebration, Vol. I", _
[Code].....
View 2 Replies
ADVERTISEMENT
Jul 10, 2009
I finally found out that my lambda in the following statement evaluates to false as opposed to the corrected version. Just wondering why:
CODE:
View 7 Replies
Feb 23, 2012
I have a user input screen that allows them to select input values and the resulting query is dependent on which values the user entered. The resulting query groups up the results to present a list of unique customers
Simplified table design - A customer can be in many states
[code...]
View 1 Replies
Feb 11, 2010
I'm using Entity Framework in Visual Studio 2010 Beta 2 (.NET framework 4.0 Beta 2). I have created an entity framework .edmx model from my database and I have a handful of many-to-many relationships.
A trivial example of my database schema is
Roles (ID, Name, Active)
Members (ID, DateOfBirth, DateCreated)
RoleMembership(RoleID, MemberID)
[Code]....
For brevity I turned the list of all the columns from the Members table into *
As you can see it's just ignoring the "Role" query.
View 2 Replies
Feb 3, 2011
Where can I find complex LINQ examples made using VB.NET Lambda Expression syntax?During my searches I always found 101 LINQ Samples but they use the other notation and for me is not always clear how to transform that code into a lambda expression.
View 1 Replies
Feb 3, 2011
I am trying to convert the following code for the variance calculation
public static double Variance(this IEnumerable<double> source){
double avg = source.Average();
double d = source.Aggregate(0.0,
(total, next) => total += Math.Pow(next - avg, 2));
return d / (source.Count() - 1);
}
Described on CodeProject into corresponded VB.NET lambda expression syntax, but I am stuck in the conversion of Aggregate function. How can I implement that code in VB.NET?
View 1 Replies
May 22, 2012
For I've tried this:Dim exampleItems As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim blah = exampleItems.Select (Function(x) New (x.Key, x.Value)).ToList 'error here
But I'm getting a syntax error and all the examples that I've seen are in C#.
View 1 Replies
Jan 12, 2011
I am pretty confused about lambdas. What I am trying to do here is write a function that will return an object from a certain table with a certain criteria. So lets say I can write:
function GetRecord(TableName as string,Criteria as string) as object
'do the linq-stuff
end function
Now I don't care if the parameters are strings or lambdas or whatever, but the end result must be that at runtime I don't know which table and which criteria will be used. As sometimes I need to get a customer record by email and sometimes a product by id etc. If possible I would prefer returning a list of matching objects and then I would just use .firstordefault when I want 1 (such as by id...).
View 2 Replies
Jan 24, 2010
I am trying to pass a lambda function to Linq's orderby and cannot get it to work.
The relevant code is:
Dim myordering = Function(m) m.Count
If isAlphaOrder Then
myOrdering = Function(m) m.Key 'Order by Alphabet
[Code].....
View 4 Replies
Apr 16, 2010
I have a lambda expression that builds a list ofanonymous types. I would like to include a property in the anonymous type that is a counter. So the first entry is 1, second 2, third 3, and so on.I thought I recallenyone know the syntax for defining an indexing variable as part of a Linq query or lambda expression?
View 10 Replies
Nov 16, 2009
I'm still trying to get my head around the whole "Lambda Expressions" thing.
Can anyone here give me an example ordering by multiple columns using VB.Net and Linq-to-SQL using a lambda expression?
Here is my existing code, which returns an ordered list using a single-column to order the results:
Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).ToList
Note: The WebCategory object has a child WebPage object (based on a foreign key). I'd like to order by WebPage.DisplayOrder first, then by WebCategory.DisplayOrder.
I tried chaining the order bys, like below, and though it compiled and ran, it didn't seem to return the data in the order I wanted.
Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).OrderBy(Function(c As WebCategory) c.WebPage.DisplayOrder).ToList
View 2 Replies
Mar 16, 2009
I have an untyped dataset returned from my WebService. The user wants to dynamically construct a query referencing tables and columns and specifying values to test for.I have looked at Lambda Expressions and the Expressions.Expression Namespace. I think these provide the answer I'm looking for, but I'm not certain how to go about contructing the linq expressions to extract the datarows I'm looking for.
I plan on limiting the result to one datatable that the query will result in and i"ll provide the joins from the user's constructs.For a simple example I have a DataTable with (n) rows called "Table", and in it there is a computed column called "b_IsActive" that has the expression "Convert(IsActive, 'System.Boolean')". The user wants to retrieve all rows in "Table" where field "b_IsActive" is true.
View 5 Replies
May 20, 2009
I'm looking for an advanced level VB.NET book which covers LINQ and Lambda Expressions.Generally I read C# .NET books due to lack of good VB.NET books when it comes to generic .NET Framework related subjects. However Lambda and LINQ is quite different in C# and VB.NET I'm looking for an advanced level VB.NET book on this subjec
View 1 Replies
Jan 26, 2012
I get the following string expression provided:"ChildObject.FullName" ...where ChildObject is an instance property on the MyObject1 type.ChildObject has a property named "FullName" and I want to sort a collection of type "MyObject1" based on this child properties "FullName" value.
I can do this all day long on properties directly on MyObject1 but I run into 2 challanges when doing it on a child instance and I can't get all the pieces working. The main 2 challanges are:
MyObject1 has a few different child property types so I can't hardcode the type for ChildObject. The string could be of any type.The sort expression is a String and not a known type.Above the value returned from the last line in the expression (if I run the code outsode the OrderBy method, does provide me the 'FullName' information I need. So the code must be close, but it still does not work.
Any ideas on how I can accomplish this? What I am trying to prevent is hardcoding a series of 'If' blocks on the child's type to then hardcode in its type to the sort or OrderBy method.
View 2 Replies
Sep 15, 2010
Can a LINQ or a LAMBDA expression be used to count the 1's in a binary string ?For example, if I convert a number to its BINARY using.>>
Dim binaryString As String = Convert.ToString( numberVariable, 2 )
1) Without using a loop such as a FOR NEXT loop, can I count the number of 1's in the STRING using LINQ or a LAMBDA expression?
2) Can I get the indexes of where the 1's are at in a collection such as a LIST using LINQ or a LAMBDA expression?I know the syntax of some LINQ expressions, however, I don't know which method may be suitable.
View 2 Replies
May 29, 2012
I found that when group by multiple values does not work well with VB.NET, but it works well with C# ,here are my code, is there something wrong with my VB.NET Code? Here is my VB.NET code:
[Code]...
View 1 Replies
Jun 29, 2010
I'm trying to use a nested multi-line lambda Function in VB.NET and am getting an error. Here's what my code looks like:
cartItems = cartItems.Select(Function(ci) New With {.CartItem = ci, .Discount = discountItems.FirstOrDefault(Function(di) di.SKU = ci.SKU)})
.Select(Function(k)
If k.Discount Is Not Nothing Then
[code]....
View 2 Replies
Jun 21, 2011
Im hearing about this, can someone explain it to me or please show some basic code of Lambda with explanation.
View 2 Replies
Dec 14, 2009
Is it possible to shorten the following function to a lambda expression?Or (to do the trick by myself) what is the best and most understandable for beginners tutorial for lambda in vb.net?[code]
View 2 Replies
Nov 22, 2009
I'm struggling with getting Linq To NHibernate to work.I have referenced NHibernate, NHibernate.Linq and NHibernate.ByteCode.Castle . Also I have all other dependencies in the same folder.
Code / Error message:
Public Function GetProjectsByName(ByVal ProjectName As String) As List(Of Project)
Return (From x In _session.Linq(Of Project)() Where x.Name.Equals(Project))
End Function[code]....
... tells me that the LINQ extensions aren't loaded. Using NHibernate.Linq seems to be made in a way that it's incredibly easy to use, hence there are no tutorials on how to set it up. (Or at least I couldn't find any).
View 1 Replies
Apr 4, 2009
linq querying various nested tables in a dataset? I can't find something like that on the net.
View 1 Replies
Jul 18, 2011
I've problems to detect if there are new rows in a DataTable Email_Total that is yet not imported into ContactDetail.
Dim srcUnique = From row In src.Email_Total
Select row.ticket_id, row.interaction, row.modified_time
Dim destUnique = From row In dest.ContactDetail
[Code].....
View 2 Replies
Aug 8, 2011
Using VB.net and the following LINQ statement. I suspect the "Order by" does not work with Concat(). I want to list the current item the user has and then list more available items in asending order. So first i select the current item from the db and then select the next available items in order. LINQ is ignoring the order by statement and sorting by the PK (which is itemID) I examined the list immediately after executing the statement. When I break up the statement and do them separately they work as predicted.
(From items In myDatabase.ItemAssignments _
Where items.BuildingID = buildingID _
And items.ResidentID = ResidentID _
[Code].....
View 1 Replies
Mar 15, 2011
I face a problem that if i make a datattype in sql server of Integer and then use it in linq to sql query it give error of "specific cast not valid" and when i change datatype into varchar the linq to sql query work fine one more thing how to use linq to sql query to make crystal report?
View 2 Replies
Sep 11, 2010
it is possible to do a multiple records editing by linqtosql method in one click event?What I've been trying to do is to edit all the names in the table which are having the same account number.
Private Sub Button2_Click(------------) Handles Button2.Click
Dim accnt As String
accnt = Textbox1.Text
Dim db As New memrecDataContext()
[code]....
Why is it these codes could only edit records in one datarow?Can you debug the codes to edit all records which are having the same account number after it is being query?
View 5 Replies
Oct 19, 2009
I would like to ask if it is possible to do a multiple records editing by linqtosql method in one click event? What I've been trying to do is to edit all the names in the table which are having the same account number. I was able to edit but only one name has been edited and the rest are not
******************************************************
Private Sub Button2_Click(------------) Handles Button2.Click
Dim accnt As String
accnt = Textbox1.Text
[Code]....
Why is it these codes could only edit records in one datarow? Can you debug the codes to edit all records which are having the same account number after it is being query?if that is possible for you.
View 3 Replies
Apr 29, 2010
i'm having troubles with a linq-to-sql statement and getting a left outer join to work. here is a sample of two tables i'm trying to join and my code that i've wrote.
Table1 Table2
f1 f2 f3 f4 fID f1 f2 f3 f4
val1 val2 val3 val4 1 val1 val2 val3 val4
[code]....
Instead I'm returning ALL values from table 1. I need to return all values from the left outer join where tbl2.fID is null.
View 1 Replies
Sep 5, 2010
I have a datagridview which is bound to a local databse table through a Linq to SQL class.
Everything works as I would expect, when the application runs, up to a point.
If, in the datagridview tasks pane I enable Adding, Editing and Deleting all work as expected.
If I disable Adding & Deleting, leaving Editing enabled, Editing works as expected.
If I disable Deleting & Editing, but leave Adding enabled, Adding does not work, despite the row for new records being displayed. I can move around the row but not enter data.
I suspect somehow its the datasource as opposed to the datagridview that is the cause of my problem but as I say, I can add rows if the Enable Editing option is selected.
Changing the relevant properties programatically makes no difference.
I've been searching for the reason for a couple of days without success.
I have a workaround, by leaving Enable Editing set and programatically setting each row during the RowPostPaint event to be readonly except the row for new records. I would just like to understand what is going on.
View 5 Replies
Aug 24, 2011
I am dumb founded at this statement....maybe its just too many hours/days of doing C# to VB.Net conversion but i am drawing a blank on this conversion.
List<string> sColors = new List<string>(this.CustomPaletteValues.Split(','));
try {
List<Color> colors = sColors.ConvertAll<Color>(s => (Color)(new ColorConverter().ConvertFromString(s)));
What i have so far:
Dim colors As List(Of Color) = sColors.ConvertAll(Of Color)(....)
As you can see its the content of the lambda that i am hitting a brick wall with.
View 3 Replies
Nov 11, 2009
I see lambda expressions have become a very useful tool at some points in the language. I've been using them a lot and most of the time they fit really nice and make the code shorter and perhaps clearer.
Now.. I've seen some , I would say excessive use of them. Some people like them so much that try to use them everywhere they can.. Some times the C# code looks like a functional language. Other factors against are the cost using reflection by lambda and that not friendly to debugging.I would like to hear opinions about how good and how code clear it is to use more or less the lambda expressions. (this is not the better example, but let's say it was the trigger)I was writing the following code. The use of the delegate { return null; } helps me avoid having to ask if the event is null or not every time I have to use it.
public delegate ContactCellInfo.Guest AddGuest();
public event AddGuest GuestRequest = delegate { return null;}
Im using resharper and the wise resharper( even it some times literaly eats the memory) made me the following suggestion
public delegate ContactCellInfo.Guest AddGuest();
public event AddGuest GuestRequest = () => null;
At my point of view the code using the delegate looks clearer. I am not against the Lamdba expression just would like to hear some advices on how and when to use them.
View 1 Replies