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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:12:00+00:00 2026-06-13T20:12:00+00:00

I’m interested in the compilation and optimization LINQ expressions and whether or not I

  • 0

I’m interested in the compilation and optimization LINQ expressions and whether or not I need to carefull consider the order of let and where clauses in my expression.

Here is example:

var query = 
    from record in Database.Table
    let recordName = record.GetName()
    let notUsed = UselessData()
    let stuff = DoSomethingIntensiveWith(record)
    where recordName == "foobar"
    select stuff;

foreach (string item in query) {
    Console.WriteLine("item => '{0}'", item);
}

Questions/assumptions:

  • record.GetName() must be resolved in order to execute the where
    clause.
  • notUsed is never used in the expression, so will UselessData() get called
    at all?
  • stuff is only needed if recordName equals “foobar”. Will
    DoSomethingIntensiveWith() be executed for every record or just records
    where recordName equals “foobar”?

If I want to ensure DoSomethingIntensiveWith() is only called when
recordName equals “foobar”, do I need to position the let caluse after the
where clause, as follows:

var query = 
    from record in Database.Table
    let recordName = record.GetName()
    let notUsed = UselessData()
    where recordName == "foobar"
    let stuff = DoSomethingIntensiveWith(record)
    select stuff;

foreach (string item in query) {
    Console.WriteLine("item => '{0}'", item);
}

In the meantime, I’m going to play with some real code and the debugger. I’ll
report back what I find.

  • 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-06-13T20:12:01+00:00Added an answer on June 13, 2026 at 8:12 pm

    If this is LINQ-to-Objects, then: yes, you do. The standard Enumerable.* implementation is very direct in terms of applying things in order. You don’t need all the let clauses necessarily, but things are still done in order, and that order is respected. If it is LINQ-to-anything-else, then all bets are off.

    This is easy to demonstrate:

    using System;
    using System.Linq;
    class Foo
    {
        public Foo(string value)
        {
            Value = value;
        }
        public string Value { get; private set; }
        public string Expensive()
        {
            Console.WriteLine(Value);
            return Value;
        }
        static void Main()
        {
            var foos = new[] {
                new Foo("abc"),
                new Foo("def")};
            Console.WriteLine("query1:");
            var query1 = (from obj in foos
                          let val = obj.Value
                          where val.StartsWith("a")
                          let result = obj.Expensive()
                          select result).ToArray();
            Console.WriteLine("query2:");
            var query2 = (from obj in foos
                          let val = obj.Value
                          let result = obj.Expensive()
                          where val.StartsWith("a")                      
                          select result).ToArray();    
        }
    }
    

    The first query filters then projects (so it only does the expensive operation for the matching records), while the second query calculates the expensive operation for both:

    query1:
    abc
    query2:
    abc
    def
    

    It should be noted that let is actually just implemented via Select – it is a projection from the source data to an anonymous type used internally to the query.

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.