What am I using? (Linq / SQL)
My project currently uses the following syntax for "linq"
var le = domainModel.EntityDataContext.Table_name_here.Query()
.WithFullReadCheck(domainModel.Security)
.Where(x => x.Metadata.AdvisorID == advisorId).FirstOrDefault();
The above code is said to be linq, unaware of linq, I decided to study it. However, at https://msdn.microsoft.com/en-us/library/gg509017.aspx , things are quite different.
what is used in my code? Is this linq version? Is this something else?
source to share
What you are using is LINQ. For the record LINQ - Lambda Syntax and Query Syntax
The difference is explained here: LINQ: Dot notation or Query Expression
There is more information about this on MSDN: https://msdn.microsoft.com/en-us/library/bb308959.aspx
The MSDN articles begin with an explanation of LINQ with SQL type syntax ("query syntax") and then further explain Lambda Expressions and expression trees ("lamba syntax").
source to share
This is the syntax for an extension method for a query. .Query().WithFullReadCheck()
much less, and the rest .Where
, and .FirstOrDefault
- quite a common extension requests. LINQ is a special syntax for expressing what you can do by chaining query extension methods. Behind the scenes, they are identical.
See this question which gives an example of LINQ method and extension syntax and has a good discussion of the differences between the two: Extension Method Syntax and Query Syntax p>
source to share
LINQ is an INtegrated language query. In practice, this means two things for most people:
The way inline delegates are converted by the compiler to expression trees rather than anonymous methods. This means it is x => x.Metadata.AdvisorID == advisorId
not compiled to IL at compile time: instead, it is compiled into code that creates an equivalent object Expression
that can be passed to a provider such as Entity Framework or LINQ to SQL to create a database query.
The other part for most people is the so-called "syntactic sugar" that effectively calls on your behalf .Where()
, .OrderBy()
etc.
from a in something
where a.Name == "Bob"
select a;
In both cases, a class Queryable
in .NET provides extension methods for building a chain of objects Expression
, such .Where()
as .OrderBy()
, .Select()
etc. - they accept IQueryable
(either untyped or with a common parameter) and return something else. They easily move the object Expression
to every point that represents the query as a whole.
Value:
someQueryable.Where(x => x.Id > 3).OrderBy(x => x.Date).Select(x => x.Ref)
Returns an implementing object IQueryable
that contains Expression
, which looks like this:
Select(OrderBy(Where(someQueryable, x => x.Id > 3), x => x.Date), x => x.Ref)
... which is read by LINQ providers to create something like:
SELECT Ref FROM someTable WHERE Id > 3 ORDER BY Date
Finally, note that .Where()
, .OrderBy()
and the like are not limited to LINQ queries / objects. They also exist for IEnumerable
(and IEnumerable<>
), but this is not LINQ, but simply performs the operations the moment the method is called.
source to share
Linq (Language Integrated Query) is the language you use to query data. You don't care where they come from, what the whole point of using it is. You can use the same code to query arrays, collections, databases, xml files, directories, etc. Linq translates your code in the background, so if you use it to, for example, fetch data from a database, it creates SQL to send to the database.
There are two syntax versions available:
1.) lambda syntax
2.) chained methods
It doesn't matter which one you choose, just try to match it and use what makes you more comfortable / makes more sense in your situation.
source to share