ToList().ForEach in Linq

You shouldn’t use ForEach in that way. Read Lippert’s “foreach” vs “ForEach” If you want to be cruel with yourself (and the world), at least don’t create useless List Note that the result of the All expression is a bool value that we are discarding (we are using it only because it “cycles” all the elements) I’ll repeat. You shouldn’t use ForEach to change objects. … Read more

LEFT OUTER JOIN in LINQ

How to perform left outer join in C# LINQ to objects without using join-on-equals-into clauses? Is there any way to do that with where clause? Correct problem: For inner join is easy and I have a solution like this but for left outer join I need a solution. Mine is something like this but it’s not working where JoinPair is a … Read more

Sequence contains no elements?

I’m currently using a single query in two places to get a row from a database. The query is fine when retrieving the row to put data in to the text boxes, but it returns an error “Sequence contains no elements” when used to retrieve the row in order to edit it and put it … Read more

LINQ’s Distinct() on a particular property

EDIT: This is now part of MoreLINQ. What you need is a “distinct-by” effectively. I don’t believe it’s part of LINQ as it stands, although it’s fairly easy to write: So to find the distinct values using just the Id property, you could use: And to use multiple properties, you can use anonymous types, which implement equality appropriately: … Read more

Join/Where with LINQ and Lambda

I find that if you’re familiar with SQL syntax, using the LINQ query syntax is much clearer, more natural, and makes it easier to spot errors: If you’re really stuck on using lambdas though, your syntax is quite a bit off. Here’s the same query, using the LINQ extension methods:

Group by in LINQ

Absolutely – you basically want: Or as a non-query expression: Basically the contents of the group (when viewed as an IEnumerable<T>) is a sequence of whatever values were in the projection (p.car in this case) present for the given key. For more on how GroupBy works, see my Edulinq post on the topic. (I’ve renamed … Read more