Coder Perfect

What are the distinctions between “LINQ to Entities,” “LINQ to SQL,” and “LINQ to Dataset”?


I’ve been using LINQ for quite some time now. However, the true differences between the LINQ flavors indicated above remain a bit of a mystery.

The effective response will provide a brief distinction between them. What is the primary purpose of each flavor, what is the benefit, and does it have an effect on performance…

P.S. I realize there are a lot of information sources available, but I’m searching for a sort of “cheat sheet” that tells a rookie where to go to achieve a specific goal.

Asked by Marcel

Solution #1

Answered by marc_s

Solution #2

LINQ is a collection of technologies that are built around a query comprehension syntax, such as:

var qry = from x in source.Foo
          where x.SomeProp == "abc"
          select x.Bar;

which the compiler translates into code:

var qry = source.Foo.Where(x => x.SomeProp == "abc").Select(x => x.Bar);

Here’s where the true magic begins. It’s worth noting that we haven’t said what Foo is – and the compiler doesn’t seem to mind! It’s satisfied as long as it can find an appropriate method named Where that can take a lambda and the result has a Select method that can accept the lambda.

Consider that the lambda can be compiled as an anonymous method (delegate in LINQ-to-Objects, which includes LINQ-to-DataSet) or an expression-tree (a runtime model that represents the lambda in an object model).

It simply performs the delegate for in-memory data (usually IEnumerableT>) – good and fast. However, the object-representation of the expression (a LambdaExpression…>) for IQueryableT> can be disassembled and applied to any “LINQ-to-Something” example.

This could involve developing TSQL for databases (LINQ-to-SQL, LINQ-to-Entities), for example:

FROM [SomeTable] x
WHERE x.SomeProp = @p1

However, it could also imply writing an HTTP query (for ADO.NET Data Services, for example).

It is faster to execute a well-written TSQL query that returns a modest amount of data than to load a complete database over the network and then filter at the client. Both, however, have optimal and plain-wrong scenarios.

The purpose and value here is to allow you to query a wide range of data sources using a single, static-checked syntax, as well as to make the code more expressive (“conventional” data grouping code, for example, isn’t very apparent in terms of what it’s attempting to achieve – it gets lost in the amount of code).

Answered by Marc Gravell

Solution #3

Language integrated query (LINQ) is an acronym for language integrated query. It allows you to extract data from data sources using “SQL style” query language directly from C#.

Linq to XML allows you to use an XML file as a data source. Linq to Objects, for example, is essentially a Collection class containing simple objects.

The word LINQ refers to the querying technology, whereas the remainder of the name refers to the data source being queried.

To give you some context, here’s some information:

Datasets are objects that load data from a database into application. After the data has been loaded, Dataset and Linq can be used to query it.

Linq to SQL allows you to classes that map to database tables, and Linq-to-SQL handles the data loading from the SQL server database.

Finally, the Entity framework is a system that allows you to establish a database and object mapping in XML and then query the data loaded via this mapping using Linq.

Answered by Simon P Stevens

Post is based on