Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6624937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:42:48+00:00 2026-05-25T21:42:48+00:00

If I were to use IEnumerable instead of var in the code example below,

  • 0

If I were to use IEnumerable instead of var in the code example below, will the SQL be generated only during the execution of the foreach statement? Or will it execute as an when the Linq statements are evaluated?

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

foreach (string name in result)   // Only now is the query executed!
   Console.WriteLine (name);

Another example:

IEnumerable<Order> query = db.Orders.Where(o => o.Amount > 1000);
int orderCount = query.Count();

Would it be better to use var (or IQueryable) as it would be executed a select count(*)… when .Count() is executed or would it be exactly same with the IEnumerable code shown above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:42:49+00:00Added an answer on May 25, 2026 at 9:42 pm

    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<Customer> query = db.OrderBy(c => c.State);  // Is narrowing rather than widening.
    query = query.Customers.Where(c => c.Age > 18); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming sr is an IEnumerable<string> , I want to use code like this to
Consider this method: public IEnumerable<T> GetList(int Count) { foreach (var X in Y) {
How much slower will work Silverlight browser app if I'll use Reflection instead of
In .NET, using foreach to iterate an instance of IEnumerable will create a copy?
Regardless of the collection type I use as input, LINQ always returns IEnumerable<MyType> instead
I have some code that currently builds up an In statement in SQL. I
I want to use LINQ to convert this IEnumerable<int>[] value1ByType = new IEnumerable<int>[3]; value1ByType[0]=
I'm a bit confused about the use of all the IEnumerable<T> extension methods, intellisense
Linq in general, has extensions methods(at IEnumerable) like Where, Select, OrderBy. But use another
Use case: 3rd party application wants to programatically monitor a text file being generated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.