Linq query syntax and extension methods

I usually prefer extension methods because they are easier for me to read, but when I saw Erno’s answer to this question , I was wondering how a minimal query would look only using extension methods?

And in general, are there any queries that you can create in one form, but not in the other, or are both approaches equivalent?

+4
source share
4 answers

You cannot do anything in query expressions that cannot be executed without query expressions - query expressions are simply converted to query code without a query. There are many queries that cannot be written in query expressions, though ... for example, using the Select overload, which also provides an index:

 var foo = bar.Select((value, index) => new { value, index }); 

... and, of course, all of the many operators that are not supported at all by query expressions ( First , etc.).

A “minimal” query would use SelectMany for the second from clause, Select for the let clause (entering a new transparent identifier), Where for the Where clause, and Select for the Select clause.

+5
source

Taken from ILSpy:

it

 var minimum = (from p1 in differenceList from p2 in differenceList let distance = Math.Abs(p1.X - p2.X) where !object.ReferenceEquals(p1, p2) orderby distance select new { Point1 = p1, Point2 = p2, Distance = distance }).First(); 

(with a little cleaning) and with comments

 var minimum = differenceList // The two from .SelectMany( p1 => differenceList, (p1, p2) => new { p1 = p1, p2 = p2 }) // The let .Select(q => new{ q = q, distance = Math.Abs(q.p1.X - q.p2.X) }) // The where .Where(r => !object.ReferenceEquals(rqp1, rqp2)) // The orderby .OrderBy(r => r.distance) // The final select .Select(r => new { Point1 = rqp1, Point2 = rqp2, Distance = r.distance }) // The First .First(); 

I have to tell the truth, the only thing I did not know how to do "manually" was two from . I suspected it was SelectMany , but it took me at least 30 minutes to crack it. If you're interested, in ILSpy Options->Decompiler and deactivate "Decompile query expressions.

+10
source

Some queries can only be written using the syntax of the extension method (in particular, there are extension methods that the query syntax does not support). The extension method syntax supports all query syntaxes because the request syntax is compiled into the same extension methods.

The query syntax, on the other hand, has several functions that are a bit more detailed in the syntax of the extension method ( let and some join s).

join can be replaced with SelectMany and let with Select , which introduces an anonymous type that includes both the actual variable in the query and the variable represented in the let clause.

A clean version in the extension method syntax would look like this:

 differenceList .SelectMany(p1=>differencelist,(p1,p2) => new {Point1 = p1,Point2 = p2, Distance=Math.Abs(q.p1.X - q.p2.X)}) .Where(e=>!object.ReferenceEquals(e.p1,e.p2)) .OrderBy(e=>e.Distance) .First(); 
+1
source

Each Linq expression can be expressed using extension methods. The compiler translates Linq to them anyway. On the other hand, not every extension method can be expressed in Linq syntax.

0
source

All Articles