Where Clause in the Entity Framework

asp.net asp.net-mvc-3 entity-framework lambda


Let's say i have a table named User. When I use the Entity Framework to get the records i do like this:

var db = new Context();
var users = db.Users;

It return all the users in my table. OK. If i do this:

var fooUsers = db.Users.Where(u => u.Name == 'foo');

It will give me all the users with name 'foo'. OK. My question is: The entity framework make a query like:

select * from user where name = 'foo'

Or it load all the users and filter them on the server with lambda expression?

11/17/2011 8:29:48 PM

Accepted Answer

The Sql submitted to your database will contain your where clause. You can use SQL Server Profiler to watch as queries are submitted to your DB.

11/17/2011 8:32:22 PM

Popular Answer

From here http://msdn.microsoft.com/en-us/library/cc853327.aspx

When you create an ObjectQuery or LINQ query, the query may not be executed immediately. Query execution is deferred until the results are needed, such as during a foreach (C#) or For Each (Visual Basic) enumeration or when it is assigned to fill a List collection. Query execution begins immediately when you call the Execute method on an ObjectQuery or when you call a LINQ method that returns a singleton query, such as First or Any. For more information, see Object Queries and Query Execution (LINQ to Entities).

So when your query has a WHERE clause it will just load the results filtered by the database with the where.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow