Var vs IEnumerable when it comes to Entity Framework?

In general, the SQL is generated when GetEnumerator is called on the IQueryable In your example, there is a subtle difference that you may want to consider. Let's take your original example: var query = db.Customers. Where (c => c.

Age > 18); query = query. Where (c => c. State == "CO"); var result = query.

Select (c => c. Name) In this case if you change your first query to IEnumerable query = ..., then the second line would use the IEnumerable version of the Where extension (LINQ to Objects) rather than the IQueryable one (LINQ to SQL/EF). As a result, when we start iterating, the first where clause is passed to the database, but the second where clause is performed on the client side (because it has already been converted to an IEnumerable) Another subtle item you want to be aware of is the following type of code: var query = db.

OrderBy(c => c. State); query = query.Customers. Where(c => c.

Age > 18); // Fails: Widening In this case, since your original query returns IOrderedQueryable rather than IQueryable. If you try to then assign query to the result of the . Where operation, you're trying to widen the scope of the return type and the compiler will refuse to perform that widening.As a result, you have to explicitly specify the baseline type rather than using var: IQueryable query = db.

OrderBy(c => c. State); // Is narrowing rather than widening. Query = query.Customers.

Where(c => c. Age > 18).

In general, the SQL is generated when GetEnumerator is called on the IQueryable. In your example, there is a subtle difference that you may want to consider. Let's take your original example: var query = db.Customers.

Where (c => c. Age > 18); query = query. Where (c => c.

State == "CO"); var result = query. Select (c => c. Name); In this case if you change your first query to IEnumerable query = ..., then the second line would use the IEnumerable version of the Where extension (LINQ to Objects) rather than the IQueryable one (LINQ to SQL/EF).

As a result, when we start iterating, the first where clause is passed to the database, but the second where clause is performed on the client side (because it has already been converted to an IEnumerable). Another subtle item you want to be aware of is the following type of code: var query = db. OrderBy(c => c.

State); query = query.Customers. Where(c => c. Age > 18); // Fails: Widening In this case, since your original query returns IOrderedQueryable rather than IQueryable.

If you try to then assign query to the result of the . Where operation, you're trying to widen the scope of the return type and the compiler will refuse to perform that widening. As a result, you have to explicitly specify the baseline type rather than using var: IQueryable query = db.

OrderBy(c => c. State); // Is narrowing rather than widening. Query = query.Customers.

Where(c => c. Age > 18).

It would make no difference. Var is just syntactic sugar. If you hover over var, you will see what type C# thinks your query is.

From msdn.microsoft.com/en-us/library/bb38397... Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of I are functionally equivalent: var I = 10; // implicitly typed int I = 10; //explicitly typed If you want to perform actions on your query that SQL wouldn't know what to do with, such as a method defined in your class, then you could use AsEnumerable().

For example: var query = db. Customers . Where(c => c.

Age > 18) .AsEnumerable() . Select(c => GetCustomerViewModel()); //put definition of GetCustomerViewModel() somewhere in the same class Update As Omar mentioned, if you want your query to be executed immediately (rather than deferred), use ToList(). This will immediately enumerate the IQueryable/IEnumerable and fill the list with the actual data from the database.

I just tested the code without .AsEnumerable() in LinqPad and it executed fine. – Omar Sep 29 at 5:30 @Omar, did you test in LinqPad using an Entity Framework DbContext? – DanM Sep 29 at 5:37.

Linq queries return IQueryable or IEnumerable, the execution of both is deferred. As DanM stated, whether or not you use var or IEnumerable it all depends on the return value of the method you're calling: if it's an IEnumerable or IQuerable it'll be deferred, if you use .ToList(), it'll be executed right away. When to use var comes down to personal choice/style.

I generally use var when the return value is understood from the line of code and variable name or if I'm instantiating a generic with a long declartion, e.g. Dictionary>. From your code, it's clear that a collection of Customers/Orders is returned, so I would use the var keyword. Again, this is a matter of personal preference.

1. Good point about ToList(). I forgot to mention that.

– DanM Sep 29 at 5:38.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions