HELP - with X# syntax - Relay Command ...
Posted: Tue Jan 24, 2017 12:15 pm
Hi Frank,
In the context of Entity Framework it's pretty much a requirement if you're going to use EF in the way it's intended.
Elsewhere in reality it's personal preference, and almost certainly slower (marginally) than doing a foreach loop. However the thing about Linq is that you can do so much more than simply iterate through lists.
Say you want to sum a value
Or get a new list of objects matching some criteria
Once you get used to the syntax it becomes a very natural way of doing just about anything you want with collections, and the method chaining is very flexible and powerful. I strongly recommend you take the time to get used to it.
Nick
In the context of Entity Framework it's pretty much a requirement if you're going to use EF in the way it's intended.
Elsewhere in reality it's personal preference, and almost certainly slower (marginally) than doing a foreach loop. However the thing about Linq is that you can do so much more than simply iterate through lists.
Say you want to sum a value
Code: Select all
decimal total = 0M;
foreach (OrderItem item in orderitemlist)
{
if (item.OrderId == requiredid)
total += item.TotalValue;
}
decimal total = orderitemlist.Where(i => i.OrderId == requiredid).Select(v => v.TotalValue).Sum();
Code: Select all
List<OrderItem> selecteditems = new List<OrderItem>();
foreach (OrderItem item in orderitemlist)
{
if (item.OrderId == requiredid)
selecteditems.Add(item);
}
List<OrderItem> selecteditems = orderitemlist.Where(i => i.OrderId == requiredid).ToList();
Nick