Is there anything that is possible in LINQ but not Lambda?

I think about the code standards for the .net project (with a harsh language), and I personally really like using lambda expressions in various extension methods on IEnumerable, etc. (Where, GroupBy, blah, blah) and don't really like the LINQ syntax.

I know that this is a personal preference, so I do not ask which is better, but for me and my team we tend to find the lambda approach more readable. So I’m thinking about always using the lambda approach in our coding standards, but I thought ...

Is there something that can only be achieved with LINQ syntax? By something, I mean that you could build a specific expression tree using LINQ, which simply could not use extension methods with lambda expressions. I think the answer is not the same as tools like resharper and linqpad can convert between them, and I was able to work out everything I have ever needed in lambda, but I wonder if there is somewhere a boundary case where smart people on here know?

+4
source share
3 answers

"Possible" and "convenient" are different; p Everything is possible; The LINQ syntax is eventually compiled into a lambda syntax, and there are features with extension syntax that are not possible in LINQ syntax ( Distinct() in C #, for example, although it exists in VB.NET LINQ).

But! When there are many variables in a LINQ query, this is a pain to display, since you will need a lot of additional predictions for intermediate objects (possibly anonymous types); for example, try to write (in lambda syntax):

 var query = from x in xSource from y in x.SomeCollection from z in zSource let foo = x.Foo + z.SomeMethod() order by foo select new {x,y,z,foo}; 

perhaps , but frankly, PITA.

+7
source

The syntax of the expression ( from x in something .. ) is converted by the compiler to the syntax of the extension method. And you can do more with extension methods than in expression syntax (although the latter can sometimes be simpler).

+1
source

LINQ experiments were actually compiled as extension methods, the compiler was updated to be able to do this, but under the hood there is an IL that does not know about this new syntax at all. Thus, the opposite is true, there are things that you can do with extension methods that you cannot do with Linq, not to mention that you can easily create new extension methods for yourself that you cannot call from "linq syntax"

0
source

Source: https://habr.com/ru/post/1387699/


All Articles