Can this statement be made into 1 lambda expression?
if (filter.SubFormId.HasValue)
{
query = query.Where(c => c.SubFormId == filter.SubFormId);
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Be very careful with this kind of query. The where clause is bound to the variable filter, not to the current value.
Original code:
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:
That crashes too.
Your version and Jared’s version also have different semantics if the filter is mutated in other ways:
Original code:
That matches every form. You never added a Where clause.
Jared’s version does something different:
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.
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