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();
source share