lambda expression with a statement body cannot be converted to anexpression tree

S

Steve Richter

Am I able to/how do I pass a multi line lambda to the Where method of
a Linq.Table? When I try it, I get the error "lambda expression with a
statement body cannot be converted to an expression tree".

( p.s. I find lambdas much easier to read in a book than to write
myself. )

private pcarDataContext db = new pcarDataContext();

// this compiles and works:
public IQueryable<AccountMasterRow> FindAccountMasterRows(
string InLikeName )
{
string s1 = InLikeName.ToLower();
return db.AccountMasterRows.Where(r => r.FullName.ToLower
().IndexOf(s1) >= 0);
}

// the multiline lambda here gives the "cannot be converted to an
expression tree" error.
public IQueryable<AccountMasterRow> FindAccountMasterRows(
string InLikeName)
{
string s1 = InLikeName.ToLower();
return db.AccountMasterRows.Where(
(r) => {
int rv = r.FullName.ToLower().IndexOf(s1);
if (rv >= 0)
return true;
else
return false;
}
) ;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top