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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:03:16+00:00 2026-05-15T04:03:16+00:00

I understand that when the C# compiler sees a linq query comprehension , it

  • 0

I understand that when the C# compiler sees a linq query comprehension, it basically does a straight translation to the corresponding Linq Extension methods and lambdas. i.e.

from x in list
select x.property

gets translated to:

list.Select(x => x.property)

my question is what do let clauses get translated to. for example how would this get translated by the compiler.

from x in list
let v = SomeComplexExpressionDependingOnx
select v

(p.s. i know this could be reduced to just select SomeComplexExpressionDependingOnx but i want to know how this is done in general)

Thanks!

  • 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-15T04:03:16+00:00Added an answer on May 15, 2026 at 4:03 am

    In this particular case, it gets translated to:

    list.Select( x => SomeComplexExpressionDependingOnx );
    

    But there may be a more complex case, such as:

    from x in list
    let v = SomeComplexExpressionDependingOnx
    where v > 10 && v+5 < 50 && SomeFunc(v) == "str"
    select x
    

    Will translate to:

    list.Where( x => 
        {
            var v = SomeComplexExpressionDependingOnx;
            return v > 10 && v+5 < 50 && SomeFunc(v) == "str";
        }
    )
    

    In other words, the let keyword is a way to minimize and/or optimize your query. That is, without the let keyword you would have to write:

    from x in list
    where
        SomeComplexExpressionDependingOnx > 10 &&
        SomeComplexExpressionDependingOnx+5 < 50 &&
        SomFunc(SomeComplexExpressionDependingOnx) == "str"
    select x
    

    Resulting in possible triple evaluation of the same expression.

    Update, following a question in comment.

    First, what’s so scary about "block expressions"? They’re just a shorthand for arbitrary delegate. That is, the following expression:

    Func<string,int> f = 
        s =>
        {
            var ln = s.Length;
            return ln/2;
        }
    

    Is equivalent to the following:

    int CompilerGeneratedMethodIdentifier0( string s )
    {
        var ln = s.Length;
        return ln/2;
    }
    
    ...
    
    Func<string, int> f = new Func<string, int>( CompilerGeneratedMethodIdentifier0 );
    

    Second, what’s so special about "block expressions"? Did you know that mmm… let’s call them "non-block" expressions also expand to the very same code? That is, the simple code new Func<string,int>( s => s.Length/2 ) is absolute equivalent to:

    int CompilerGeneratedMethodIdentifier0( string s )
    {
        return s.Length/2;
    }
    
    ...
    
    new Func<string, int>( CompilerGeneratedMethodIdentifier0 );
    

    Third, what’s so non-linqy about "block expressions"? LINQ uses delegates all over the place, and it doesn’t really matter to LINQ what exact shortcut you use to represent those delegates.

    In particular, your expression from a in list where a.SomeProp > 10 select new { A = a, B = a.GetB() } gets translated into the following:

    class AnonymousType0
    {
        public MyClass A { get; set; }
        public othertype B { get; set; }
    }
    
    bool WhereFunc0( MyClass a )
    {
        return a.SomeProp > 10;
    }
    
    AnonymousType0 SelectResultFunc0( MyClass a )
    {
        AnonymousType0 result = new AnonymousType0();
        result.A = a;
        result.B = a.GetB();
        return result;
    }
    
    ...
    
    list
        .Where( new Func<MyClass,bool>( WhereFunc0 ) )
        .Select( new Func<MyClass,AnonymousType0>( SelectResultFunc0 ) );
    

    Fourth, to get understanding like this, one can just play with the language and explore.

    And fifth, if the previous advice doesn’t work for you for one reason or another, you always have ILSpy. Very useful tool, everybody should have one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.