Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

14
  • 18
    I knew the best answer would be posted by Jon Skeet simply by reading the title of the post. If its got anything to do with LINQ, Skeet's your man. Read 'C# In Depth' to attain God-like linq knowledge. Commented Jan 16, 2014 at 3:03
  • 2
    great answer!!! also, for all VB_Complainers about the yield + extra lib, foreach can be re-written as return source.Where(element => knownKeys.Add(keySelector(element))); Commented Mar 4, 2014 at 16:51
  • 6
    @sudhAnsu63 this is a limitation of LinqToSql (and other linq providers). The intent of LinqToX is to translate your C# lambda expression into the native context of X. That is, LinqToSql converts your C# into SQL and executes that command natively wherever possible. This means any method that resides in C# can't be "passed through" a linqProvider if there's no way to express it in SQL (or whatever linq provider you're using). I see this in extension methods to convert data objects to view models. You can work around this by "materializing" the query, calling ToList() before DistinctBy(). Commented Nov 16, 2015 at 22:18
  • 3
    @Shimmy: I'd certainly welcome that... I'm not sure what the feasibility is. I can raise it in the .NET Foundation though... Commented May 22, 2017 at 11:39
  • 2
    @Shimmy: The answer from Carlo may work in LINQ to SQL... I'm not sure. Commented Sep 3, 2019 at 5:37