Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I usually use the term entity to represent a business data object and in my mind, the linq to entities and linq to objects were the same. Is that not correct?

like image 833
Alex J Avatar asked Aug 25 '11 14:08

Alex J


People also ask

What is the difference between LINQ to object and LINQ?

The main difference between LINQ to SQL and LINQ to Objects is that LINQ to SQL requires a data context object that works as the bridge between LINQ and the database, whereas LINQ to Objects does not require any intermediate LINQ provider or API.

What is the difference between LINQ and Entity Framework?

Entity Framework is an object-relational mapping (ORM) framework for connecting C# code to external databases, usually SQL Server. LINQ is a query language embedded into C# and a set of extension methods in order to make it useful.

Which is correct about 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.

What is LINQ to objects in C#?

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>.


2 Answers

That is definitely not the case.

LINQ-to-Objects is a set of extension methods on IEnumerable<T> that allow you to perform in-memory query operations on arbitrary sequences of objects. The methods accept simple delegates when necessary.

LINQ-to-Entities is a LINQ provider that has a set of extension methods on IQueryable<T>. The methods build up an expression tree (which is why delegates are actually passed as Expression<>s), and the provider will build up a SQL query based on its parsing of that expression tree.

As an example, consider the following queries:

var query1 = mydb.MyEntity.Select(x => x.SomeProp).Where(x => x == "Prop"); var query2 = mydb.MyEntity.Select(x => x.SomeProp).AsEnumerable().Where(x => x == "Prop"); 

The first query is will build up an expression tree consisting of a select and a where, with the two lambdas actually considered as LambdaExpressions. The LINQ-to-Entities provider will translate that into SQL that both selects and filters.

The second query inserts an AsEnumerable(), which will force the remainder of the query to use LINQ-to-Objects. In that case, the provider will generate SQL based on only the selection, return all those records from the database, and then the filtering will occur in-memory. Obviously, that's likely going to be much slower.

like image 132
dlev Avatar answered Sep 18 '22 22:09

dlev


L2o is for in-memory objectes. L2e queries a database.

like image 41
msfanboy Avatar answered Sep 19 '22 22:09

msfanboy