3

My project currently uses for following syntax for "linq"

 var le = domainModel.EntityDataContext.Table_name_here.Query()
                    .WithFullReadCheck(domainModel.Security)
                    .Where(x => x.Metadata.AdvisorID == advisorId).FirstOrDefault();

It is said the above code is linq, being unaware about linq I decided to learn it. However, on https://msdn.microsoft.com/en-us/library/gg509017.aspx , things are pretty different.

what is being used in my code? Is it a version of linq? Is it something else?

Indigo
  • 777
  • 2
  • 13
  • 25
  • 4
    I thought both were LINQ, one being syntactic sugar for the other. (The one you linked, no pun intended, is syntactic sugar that generates something like what you posted here) – Borgleader May 26 '15 at 20:59
  • Actually, given how "LINQ" has become a bit of a buzzword in the C# world (to the point that sometimes, every use of extension method is called "LINQ"), I think this is quite a good question. – O. R. Mapper May 26 '15 at 21:02
  • @Borgleader I don't get the unintended pun. Edit: oh, wow I'm slow. – SimpleVar May 26 '15 at 21:03

4 Answers4

5

What you are using is LINQ. There are 2 different notations you can use for writing LINQ - Lambda Syntax and Query Syntax

The difference is explained here: LINQ: Dot Notation vs Query Expression

There is more information on this on MSDN here: https://msdn.microsoft.com/en-us/library/bb308959.aspx

The MSDN articles starts explaining LINQ with SQL-like syntax ("query syntax") and then goes on to explain Lambda Expressions and Expression Trees ("lamba syntax").

Community
  • 1
  • 1
Russ Penska
  • 126
  • 1
  • 6
  • 2
    This answer would be better with an authoritative (read: published by Microsoft) source that explictly states "LINQ" is *not* the SQL-like syntax (or at least a Microsoft-produced LINQ example that uses the lambda syntax). – O. R. Mapper May 26 '15 at 21:09
  • 1
    Thank you - updated to reference MSDN article which shows the use of both variations. – Russ Penska May 26 '15 at 21:18
1

That is extension method syntax for a query. The .Query().WithFullReadCheck() are much less common, but the rest .Where and .FirstOrDefault are pretty common query extensions. LINQ is a special syntax for expressing the same thing that can be done with chaining the query extension methods. Behind the scenes, they are identical.

See this question which gives an example of both LINQ and extension method syntax and has a good discussion of the differences between them: Extension methods syntax vs query syntax

Community
  • 1
  • 1
AaronLS
  • 37,329
  • 20
  • 143
  • 202
1

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.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
1

Linq (language integrated query) is the language you use to query the data. You don't care where do they come from, that's the whole point of using it. You can use the same code to query arrays, collections, databases, xml files, directories, whatever... Linq translates your code in the background, so if you use it for instance to get the data from a database, it produces SQL to be sent to the database.

There are two syntax versions available:

1.) lambda syntax
2.) chainable methods

It doesn't matter which one you chose, just try to be consistent with it and use what makes you more comfortable / makes more sense in your situation.

walther
  • 13,466
  • 5
  • 41
  • 67