7

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?

tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
  • There were a similiar question and a [good answer](http://stackoverflow.com/a/4703001/1177964) for it. – Fedor Jan 17 '14 at 12:08

4 Answers4

4

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

BenjaminPaul
  • 2,931
  • 19
  • 18
3

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());
StevieB
  • 982
  • 7
  • 15
3

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.

phil soady
  • 11,043
  • 5
  • 50
  • 95
2

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.

Mashton
  • 6,037
  • 2
  • 25
  • 35