By Alex J

2011-08-25 14:23:10 8 Comments

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?


@JSJ 2011-08-25 14:32:19

Linq to Objects The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable or IEnumerable 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, Array, or Dictionary. The collection may be user-defined or may be returned by a .NET Framework API.

In a basic sense, LINQ to Objects represents a new approach to collections. In the old way, you had to write complex foreach loops that specified how to retrieve data from a collection. In the LINQ approach, you write declarative code that describes what you want to retrieve.

ref :

Linq to Entity 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. The following is the process for creating and executing a LINQ to Entities query: Ref :

@msfanboy 2011-08-25 14:32:14

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

@Jan 2011-08-25 14:25:38

Linq2Entities stands for querying data via the ADO.NET Entity Framework (Database)

Linq2Objects stands for querying in memory data (local objects).

@dlev 2011-08-25 14:25:37

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.

@Alex J 2011-08-25 14:38:34

is there any way to filter on both conditions for a linq to object query or is it always narrowing down to the first action before processing the next?

@dlev 2011-08-25 14:43:26

It depends; if you just want to get all the records from a database and then perform actions on them in memory, you can put the AsEnumerable() before the Select() and then do what you want. In general, though, it's a better idea to keep things separate. If you have an IEnumerable<T>, then you'll be using LINQ-to-Objects. If not, then the database will likely be more efficient.

@Stav Alfi 2012-10-08 09:56:57

@dlev you didn't answer what he asked.

@onof 2011-08-25 14:24:30

Linq to entities is intended to be used with Entity Framework while Linq to objects is for any IEnumerable collection.

More details:

  1. Linq to Entities
  2. Linq to Objects

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] Update all objects in a collection using LINQ

  • 2008-12-29 22:15:23
  • lomaxx
  • 444075 View
  • 447 Score
  • 16 Answer
  • Tags:   c# .net linq foreach

23 Answered Questions

[SOLVED] LINQ query on a DataTable

45 Answered Questions

[SOLVED] Deep cloning objects

  • 2008-09-17 00:06:27
  • NakedBrunch
  • 760967 View
  • 2096 Score
  • 45 Answer
  • Tags:   c# .net clone

34 Answered Questions

[SOLVED] What is the Java equivalent for LINQ?

  • 2009-08-01 18:53:26
  • Ahmed
  • 263209 View
  • 790 Score
  • 34 Answer
  • Tags:   java linq

7 Answered Questions

[SOLVED] Multiple "order by" in LINQ

  • 2008-11-18 13:34:11
  • Sasha
  • 566430 View
  • 1519 Score
  • 7 Answer
  • Tags:   linq sql-order-by

9 Answered Questions

[SOLVED] Group by in LINQ

  • 2011-09-06 19:44:20
  • test123
  • 1181930 View
  • 982 Score
  • 9 Answer
  • Tags:   c# linq group-by

296 Answered Questions

[SOLVED] Hidden Features of C#?

  • 2008-08-12 16:32:24
  • Serhat Ozgel
  • 673369 View
  • 1475 Score
  • 296 Answer
  • Tags:   c# hidden-features

20 Answered Questions


  • 2010-08-04 11:18:21
  • Toy
  • 615689 View
  • 492 Score
  • 20 Answer
  • Tags:   c# linq join

19 Answered Questions

[SOLVED] Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>

9 Answered Questions

[SOLVED] Learning about LINQ

Sponsored Content