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 3229778
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:51:00+00:00 2026-05-17T16:51:00+00:00

For example: var query = from c in db.Cars select c; foreach(Car aCar in

  • 0

For example:

var query = from c in db.Cars select c;
foreach(Car aCar in query)
{
     Console.WriteLine(aCar.Name);
}

How would this translate once it is compiled? What happens behind the scenes?

  • 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-17T16:51:01+00:00Added an answer on May 17, 2026 at 4:51 pm

    It is compiled in the following way:

    1. First, the LINQ query expression is transformed into method calls:

      public static void Main()
      {
          var query = db.Cars.Select<Car, Car>(c => c);
          foreach (Car aCar in query)
          {
               Console.WriteLine(aCar.Name);
          }
      }
      
    2. If db.Cars is of type IEnumerable<Car> (which it is for LINQ-to-Objects), then the lambda expression is turned into a separate method:

      private Car lambda0(Car c)
      {
          return c;
      }
      private Func<Car, Car> CachedAnonymousMethodDelegate1;
      public static void Main()
      {
          if (CachedAnonymousMethodDelegate1 == null)
              CachedAnonymousMethodDelegate1 = new Func<Car, Car>(lambda0);
          var query = db.Cars.Select<Car, Car>(CachedAnonymousMethodDelegate1);
          foreach // ...
      }
      

      In reality the method is not called lambda0 but something like <Main>b__0 (where Main is the name of the containing method). Similarly, the cached delegate is actually called CS$<>9__CachedAnonymousMethodDelegate1.

      If you are using LINQ-to-SQL, then db.Cars will be of type IQueryable<Car> and this step is very different. It would instead turn the lambda expression into an expression tree:

      public static void Main()
      {
          var parameter = Expression.Parameter(typeof(Car), "c");
          var lambda = Expression.Lambda<Func<Car, Car>>(parameter, new ParameterExpression[] { parameter }));
          var query = db.Cars.Select<Car, Car>(lambda);
          foreach // ...
      }
      
    3. The foreach loop is transformed into a try/finally block (this is the same for both):

      IEnumerator<Car> enumerator = null;
      try
      {
          enumerator = query.GetEnumerator();
          Car aCar;
          while (enumerator.MoveNext())
          {
              aCar = enumerator.Current;
              Console.WriteLine(aCar.Name);
          }
      }
      finally
      {
          if (enumerator != null)
              ((IDisposable)enumerator).Dispose();
      }
      
    4. Finally, this is compiled into IL the expected way. The following is for IEnumerable<Car>:

      // Put db.Cars on the stack
      L_0016: ldloc.0 
      L_0017: callvirt instance !0 DatabaseContext::get_Cars()
      
      
      // “if” starts here
      L_001c: ldsfld Func<Car, Car> Program::CachedAnonymousMethodDelegate1
      L_0021: brtrue.s L_0034
      L_0023: ldnull 
      L_0024: ldftn Car Program::lambda0(Car)
      L_002a: newobj instance void Func<Car, Car>::.ctor(object, native int)
      L_002f: stsfld Func<Car, Car> Program::CachedAnonymousMethodDelegate1
      
      
      // Put the delegate for “c => c” on the stack
      L_0034: ldsfld Func<Car, Car> Program::CachedAnonymousMethodDelegate1
      
      
      // Call to Enumerable.Select()
      L_0039: call IEnumerable<!!1> Enumerable::Select<Car, Car>(IEnumerable<!!0>, Func<!!0, !!1>)
      L_003e: stloc.1
      
      
      // “try” block starts here
      L_003f: ldloc.1 
      L_0040: callvirt instance IEnumerator<!0> IEnumerable<Car>::GetEnumerator()
      L_0045: stloc.3
      
      
      // “while” inside try block starts here
      L_0046: br.s L_005a
      L_0048: ldloc.3   // body of while starts here
      L_0049: callvirt instance !0 IEnumerator<Car>::get_Current()
      L_004e: stloc.2 
      L_004f: ldloc.2 
      L_0050: ldfld string Car::Name
      L_0055: call void Console::WriteLine(string)
      L_005a: ldloc.3   // while condition starts here
      L_005b: callvirt instance bool IEnumerator::MoveNext()
      L_0060: brtrue.s L_0048  // end of while
      L_0062: leave.s L_006e   // end of try
      
      
      // “finally” block starts here
      L_0064: ldloc.3 
      L_0065: brfalse.s L_006d
      L_0067: ldloc.3 
      L_0068: callvirt instance void IDisposable::Dispose()
      L_006d: endfinally 
      

      The compiled code for the IQueryable<Car> version is also as expected. Here is the important part that is different from the above (the local variables will have different offsets and names now, but let’s disregard that):

      // typeof(Car)
      L_0021: ldtoken Car
      L_0026: call Type Type::GetTypeFromHandle(RuntimeTypeHandle)
      
      
      // Expression.Parameter(typeof(Car), "c")
      L_002b: ldstr "c"
      L_0030: call ParameterExpression Expression::Parameter(Type, string)
      L_0035: stloc.3 
      
      
      // Expression.Lambda(...)
      L_0036: ldloc.3 
      L_0037: ldc.i4.1           // var paramArray = new ParameterExpression[1]
      L_0038: newarr ParameterExpression
      L_003d: stloc.s paramArray
      L_003f: ldloc.s paramArray
      L_0041: ldc.i4.0                    // paramArray[0] = parameter;
      L_0042: ldloc.3 
      L_0043: stelem.ref 
      L_0044: ldloc.s paramArray
      L_0046: call Expression<!!0> Expression::Lambda<Func<Car, Car>>(Expression, ParameterExpression[])
      
      
      // var query = Queryable.Select(...);
      L_004b: call IQueryable<!!1> Queryable::Select<Car, Car>(IQueryable<!!0>, Expression<Func<!!0, !!1>>)
      L_0050: stloc.1 
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am converting a c# LINQ example: var query = from m in typeof(string).GetMethods()
for example this code var html = <p>This text is <a href=#> good</a></p>; var
For some reason I'm not getting this. (Example model below) If I write: var
Example: select ename from emp where hiredate = todate('01/05/81','dd/mm/yy') and select ename from emp
Is it possible to use custom method In query for example: var result =
I have found this example on StackOverflow: var people = new List<Person> { new
Say for example I have the following string: var testString = Hello, world; And
I try to make a query to my database with LINQ to SQL. example
Example: You have a shortcut s to SomeProgram in the current directory. In cmd.exe
Example I have Person , SpecialPerson , and User . Person and SpecialPerson are

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.