Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return from a linq where statement

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.

like image 648
Vaccano Avatar asked Mar 11 '10 20:03

Vaccano


People also ask

How do I return a LINQ query result as a specific type?

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.

What does select in LINQ return?

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.

How to query in LINQ?

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 ).

How to write a Select query in LINQ?

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.


2 Answers

Does this work?

MyLinqToSQLTable.Where(x =>  
{   
    return x.objectID == paramObjectID;  
}).ToList();  
like image 30
Jacob Seleznev Avatar answered Sep 29 '22 22:09

Jacob Seleznev


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-body

anonymous-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.


Update:

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>>).

like image 82
mmx Avatar answered Sep 29 '22 23:09

mmx