I have the following link function
MyLinqToSQLTable.Where(x => x.objectID == paramObjectID).ToList();
I most of the time you can change a linq call to be several lines by adding curly brackets around the method body. Like this:
MyLinqToSQLTable.Where(x =>
{
x.objectID == paramObjectID;
}).ToList();
Problem is the implied return that was there when I just did a Boolean compare is now not done. Return (x.objectID == paramObjectID); is not accepted either.
How do do this? can I do this?
NOTE: I know that I can add another where clause if needed. But I would still like to know the answer to this.
By default, LINQ queries return a list of objects as an anonymous type. You can also specify that a query return a list of a specific type by using the Select clause.
The Select() method invokes the provided selector delegate on each element of the source IEnumerable<T> sequence, and returns a new result IEnumerable<U> sequence containing the output of each invocation.
In a LINQ query, the first step is to specify the data source. In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from clause comes first in order to introduce the data source ( customers ) and the range variable ( cust ).
The select query in LINQ to SQL is used to get all the records or rows from the table. LINQ to SQL select query can be used to filter the records of the table with the where clause. Here, we can also perform multiple operations like grouping, joining, etc. using LINQ to SQL select query based on our requirement.
Does this work?
MyLinqToSQLTable.Where(x =>
{
return x.objectID == paramObjectID;
}).ToList();
Your first query is equivalent to this one:
MyLinqToSQLTable.Where(x =>
{
return x.objectID == paramObjectID;
}).ToList();
You are missing the return
keyword here. It's necessary when the lambda body is an explicit block rather than an expression.
The spec formally defines lambda-expression in grammar like:
lambda-expression:
anonymous-function-signature => anonymous-function-bodyanonymous-function-body:
expression
block
The former case (expression) applies when the body doesn't begin with a left curly brace. The latter case (block) is defined as a series of statements (just like a method body). Like other places in C#, expression statements in a block are restricted to declarations, assignments, function call, increment, and decrement. Merely applying operator ==
to a couple identifiers doesn't the expression a valid statement. The second issue is that when the return type of a method (anonymous or not) is not void
, all code paths reaching the end of the block should return a value. Consequently, even if the body of your lambda was syntactically valid, without a return statement, your lambda would be convertible to Action<T>
, and not Func<T, bool>
that Where
method expects.
Problem is the implied return that was there when I just did a Boolean compare is now not done. Return (x.objectID == paramObjectID); is not accepted either.
Of course, the x => { return x.objectID == paramObjectID; }
variant of your lambda expression is only possible when it's supposed to be converted to an anonymous method, not an expression tree. That is, a lambda with a block body is not convertible to Expression<T>
. That's why you can use it in LINQ to Objects (in which Where
takes Func<T, bool>
) but you can't use it in LINQ to SQL (in which Where
takes Expression<Func<T, bool>>
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With