LINQ is 'Language INtegrated Query'. In practical terms it means two things to most people:
The way that inline delegates are converted by the compiler into expression trees, not anonymous methods. That means that x => x.Metadata.AdvisorID == advisorId
is not compiled into IL at compile time: instead it's compiled into code which builds an equivalent Expression
object, which can be passed to a provider such as Entity Framework or LINQ to SQL, in order to generate a database query.
The other part it means to most people is the so-called "syntactic sugar", which effectively calls .Where()
, .OrderBy()
etc on your behalf:
from a in something
where a.Name == "Bob"
select a;
In both cases, the Queryable
class in .NET provides extension methods for building a chain of Expression
objects, for example .Where()
, .OrderBy()
, .Select()
etc - these take an IQueryable
(either untyped or with a generic parameter) and return another. They lightly wrap an Expression
object at each point which represents the query as a whole.
Meaning:
someQueryable.Where(x => x.Id > 3).OrderBy(x => x.Date).Select(x => x.Ref)
Returns an object implementing IQueryable
which holds an Expression
which looks like:
Select(OrderBy(Where(someQueryable, x => x.Id > 3), x => x.Date), x => x.Ref)
... which is readable by LINQ providers to produce something like:
SELECT Ref FROM someTable WHERE Id > 3 ORDER BY Date
Finally, note that .Where()
, .OrderBy()
and the like are NOT limited to queryable/LINQ objects. They exist for IEnumerable
(and IEnumerable<>
), too - but this is not LINQ, and simply performs the operations at the instant that the method is called.