Linq To Objects vs Linq To Entities vs Entity Framework

Published on Friday, May 25, 2012

There are some articles in the internet confused the concepts among Linq to Objects, Linq to Entities and even ADO.NET Entity Framework, they often mentioned Linq To Objects and Linq to Entity or Entity Framwork are the same ting. But they ARE NOT the same.

Linq To Object:

“The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML. You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey, TValue>. The collection may be user-defined or may be returned by a .NET Framework API.” – From Microsoft.

Linq to Objects can have Linq to String, Linq To Arrary….

For example:

query = from Student student in arrList
        where student.Scores[0] > 95
        select student;

        foreach (Student s in query)
           Console.WriteLine(s.LastName + ": " + s.Scores[0]);

Linq to Entities:

LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. Queries against the Entity Framework are represented by command tree queries, which execute against the object context. LINQ to Entities converts Language-Integrated Queries (LINQ) queries to command tree queries, executes the queries against the Entity Framework, and returns objects that can be used by both the Entity Framework and LINQ.  – From Microsoft

For Example:

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<Product> productsQuery = from product in context.Products
                                        select product;

    Console.WriteLine("Product Names:");
    foreach (var prod in productsQuery)
    {
        Console.WriteLine(prod.Name);
    }
}

ADO.NET Entity Framework (or Call Entity Framework):

Entity Framework is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.

The ADO.NET Entity Framework enables developers to create data access applications by programming against a conceptual application model instead of programming directly against a relational storage schema. The goal is to decrease the amount of code and maintenance required for data-oriented applications. Entity Framework applications provide the following benefits

– From Microsoft.

Simple Differences:

Linq to Objects if for querying in memory data (local objects).

Linq to Entities is for querying data via the ADO.NET Entity Framework (Database)

Official Microsoft WebPages:

Linq To Objects (Visual Studio 2010)

Linq to Entities (.NET 4.0)

ADO.NET Entity Framework (.NET 4.0)

Entity Framework (Data Developer Center)

Other sources:

Linq to entities vs linq to objects - are they the same?

Wikipedia – ADO.NET Entity Framework