tacos_tacos_tacos
tacos_tacos_tacos

Reputation: 10585

Does Entity Framework perform lambda expressions before or after it fetches the data?

My belief has always been that if BigDataSet is an entity and you assign var someThing = Context.BigDataSet.Single(x => x.Name.Trim().ToUpper.Equals(someName.Trim().ToUpper())); then EF will do some magic to translate your lambda expression into a query, like

SELECT * FROM big_data_set WHERE name = (someName)

But after thinking about it, I can't see a way that it works except by keeping the results of Context.BigDataSet in memory and then performing the search.

Do EF DbSet lambda expressions get turned into SQL, or is it all applied after-the-fact?

Edit: If they do get turned into SQL, how does EF get to "see" the entire stack of things that are going to happen after it fetches the name? If I call instance.MethodA().MethodB(), doesn't that usually mean methods A and B are executed sequentially?

Upvotes: 7

Views: 2040

Answers (4)

phil soady
phil soady

Reputation: 11338

EF provider implementations. get Expression trees not lambdas. More about expressions in EF After thinking about your question more, my guess is you didnt realise about Expressions used. The difference between Expression<Function<t,bool>> predicate and just Func<t,bool> predicate. Is important. A provider would NOT be able to use func. It needs an expression tree.

There is a subset of LINQ that can be used in expressions based on the Linq to entities standard.supported queries

try in in debugger and look at the content of a variable of type Expression<Func<t,bool>> . You well see how the lambda has been converted to an expression and it is this expression that the DB provider uses to convert to SQL.

Upvotes: 3

StevieB
StevieB

Reputation: 1000

They're translated into SQL when called directly on the entity.

If you want to cache the result locally so that you're not hitting the database on every query of an entity you can use immediate execution e.g. via ToList() and then run multiple queries on these cached values. Evidently this hits the Database straight away but just the once.

As with LINQ queries in general the work doesn't take place (i.e. no hit to the database) until you start iterating over the resulting IEnumerable.

You may better understand the second part of your question by viewing the SQL generated from your query e.g. by trying:

var query = Context.BigDataSet.Single(x => x.Name.Trim().ToUpper.Equals(someName.Trim().ToUpper()));
var objectQuery=query as System.Data.Objects.ObjectQuery;
Console.WriteLine(objectQuery.ToTraceString());

Upvotes: 3

BenjaminPaul
BenjaminPaul

Reputation: 2931

Entity Framework does not hit the database until you iterate over your query, essentially its returning IQueryable's which are then converted to a SQL query at the point when you itterate.

var query = context.Employees; // Returns IQueryable<Employee>

var employees = context.Employees.ToList(); // Returns List<Employee> (Hits the database)

If you want a comprehensive explanation you can google "Entity Framework Deferred Execution".

Upvotes: 4

Mashton
Mashton

Reputation: 6415

They get turned into SQL unless you are performing the lambda operations on a stored procedure function mapping. You can use an SQL profiler (or EF profiler) to see this yourself, and peek at the SQL that is generated when a query is run.

Upvotes: 2

Related Questions