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

  • Home
  • SEARCH
  • 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 6886977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:49:39+00:00 2026-05-27T05:49:39+00:00

Can this statement be made into 1 lambda expression? if (filter.SubFormId.HasValue) { query =

  • 0

Can this statement be made into 1 lambda expression?

if (filter.SubFormId.HasValue)
{
    query = query.Where(c => c.SubFormId == filter.SubFormId);
}
  • 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-27T05:49:39+00:00Added an answer on May 27, 2026 at 5:49 am

    Be very careful with this kind of query. The where clause is bound to the variable filter, not to the current value.

    Original code:

    var query = from form in forms select form;
    var filter = new Filter();
    filter.SubFormId = 123;
    if (filter.SubFormId.HasValue)
    {
        query = query.Where(c => c.SubFormId == filter.SubFormId);
    }
    filter = null;
    foreach(var matchingForm in query)
    { ... }
    

    That code crashes. The query uses the current value of filter, not the value that it was when the query was made.

    Jared’s version does no better:

    var query = from form in forms select form;
    var filter = new Filter();
    filter.SubFormId = 123;
    query = query.Where(c => 
      !filter.SubFormId.HasValue || c.SubFormId == filter.SubFormId);
    filter = null;
    foreach(var matchingForm in query)
    { ... }
    

    That crashes too.

    Your version and Jared’s version also have different semantics if the filter is mutated in other ways:

    Original code:

    var query = from form in forms select form;
    var filter = new Filter();
    filter.SubFormId = null;
    if (filter.SubFormId.HasValue)
    {
        query = query.Where(c => c.SubFormId == filter.SubFormId);
    }
    filter.SubFormId = 123;
    foreach(var matchingForm in query)
    { ... }
    

    That matches every form. You never added a Where clause.

    Jared’s version does something different:

    var query = from form in forms select form;
    var filter = new Filter();
    filter.SubFormId = null;
    query = query.Where(c => 
      !filter.SubFormId.HasValue || c.SubFormId == filter.SubFormId);
    filter.SubFormId = 123;
    foreach(var matchingForm in query)
    { ... }
    

    That matches only the forms that have subformId equal to 123. Jared’s version matches against the current value of the filter subform Id if there is one, regardless of whether or not there was one when the query was built.

    Remember, queries are objects that represent the query. When you execute the query, the query is re-executed from scratch and the clauses in it are evaluated against the current values of the variables that the query closed over, not against a snapshot of the values of the variables as they existed when the query was created. Queries have a live, up-to-date view of variables.

    So lets say I also return this query, which allows the query to be evaluated outside of the method. Would this crash since the filter will go out of scope?

    Short answer:

    no.

    Longer answer:

    This is a duplicate of this question:

    Action/Lambda Expression Memory Management Question

    See my answer for details.

    Long answer:

    You are confusing scope with lifetime — a common error. Scope is purely a compile-time concept; the scope of a local variable is the region of program text in which that variable may be referred to by its name. The lifetime of a variable is purely a runtime concept; the lifetime of a local variable is the period of time during which the storage is known to the garbage collector to be valid.

    Scope and lifetime are often connected; while control is logically in a portion of the program text in which a local is in scope, it is known to be alive. Locals used in queries, lambdas, iterator blocks and async blocks have their lifetimes extended so that even when control leaves the scope of the local, the local is still alive. The local stays alive at least until the query, lambda, etc, is dead.

    It is possible in some cases that the local stays alive longer, unfortunately; see the linked question above for an example. For an extended discussion of this problem, see this question:

    C# Action, Closure, and Garbage Collection

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

Sidebar

Related Questions

Can anybody explain me this statement! pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
Is this statement, “i see him last night” can be understood as “I saw him last night”?
I can't quite figure out why this Linq Statement isn't working as i would
Hi can someone tell me what i'm doing wrong in this jquery statement. I
Can we initialize Python objects with statement like this: a = b = c
How can I do this without having a million if then statements?
Does MySQLdb support server-side prepared statements ? I can't figure this out from its
Can this be done by setting a property? I'd prefer that approach then to
Can this be done w/ linqtosql? SELECT City, SUM(DATEDIFF(minute,StartDate,Completed)) AS Downtime FROM Incidents GROUP
Can this be done? It seems like this should be possible. In general, I

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.