Reputation: 1528
Is LinqToSQL syntax same as LinqToEntity or any other LINQ_TO? Is there any additional syntax(usable methods) that can be used when we process for example data from SQL server that cannot be used in XML data or array data?
I understand that I can use on all datasources methods shown here: http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
but are there some methods that are specific to some LINQ providers?
Or it is safe to say that LINQ syntax is 100% compatible within all providers?
Upvotes: 1
Views: 49
Reputation: 156524
The C# syntax itself is the same: It's all LINQ, which is just a part of C#. However, the API you use, and the methods you are allowed to call, can differ from one LINQ Provider to the next.
For example, in LINQ to Objects, you can call pretty much any methods that are defined in any C# libraries. However, Entity Framework and LINQ to SQL try to convert your method calls into SQL syntax, so if they haven't been explicitly programmed to know how to convert a given method or operation, you'll get a runtime error when they try to execute the query.
LINQ to XML is effectively backed by LINQ to Objects--I don't think it uses its own query provider--but the API that it provides allows you to treat XML elements as if they are objects. You won't find methods like .Elements()
if you're querying, e.g. a collection in Entity Framework.
Most "LINQ to [X]" frameworks are based on you calling extension methods on IQueryable<>
or IEnumerable<>
objects, but you can enable LINQ query expression syntax on any object by implementing methods like .Where()
and .Select()
. For example, this Maybe type implements .Where()
and .Select()
so you can say:
from m in Maybe.From(GetValue())
where m.IsCool
select m.Name
But it doesn't implement the IEnumerable<>
interface, so methods like GroupBy aren't implemented. If you try to use group x by y
syntax in a query on a Maybe<>
, the compiler will complain.
Upvotes: 1
Reputation: 223267
You are mixing LINQ and ORM.
LINQ provides querying facility over collection of objects, IEnuerable<T>
, IQueryable<T>
.
ORM provides functionality to talk to database through objects.
Some methods like Last
/LastOrDefault
, TakeWhile
doesn't work with LINQ to SQL, since LINQ to SQL is responsible for translating LINQ query to under laying data source language i.e. SQL.
You may see: Standard Query Operator Translation
Similarly, there could be LINQ methods which are supported for in memory collection but are not supported for other providers. This will depend on each provider.
Upvotes: 1