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

R

Ramkrishnan .

hi,

i got an error "A lambda expression with a statement body cannot be
converted to an expression tree" when i write below code.

data = is the iquerable object.

how to solve tis error.



data.Where(
item =>
{
object oValue =
item.GetType().GetProperty(field).GetValue(item, null);
IComparable cItem = oValue as IComparable;

switch (comparison)
{
case Comparison.Eq:

switch (type)
{
case FilterType.List:
return !(value as
System.Collections.ObjectModel.ReadOnlyCollection<string>).Contains(oValue.ToString());
case FilterType.String:
return !
oValue.ToString().StartsWith(value.ToString());
default:
return !
cItem.Equals(value);
}

case Comparison.Gt:
return cItem.CompareTo(value) < 1;
case Comparison.Lt:
return cItem.CompareTo(value) >
-1;
default:
throw new
ArgumentOutOfRangeException();
}
}
);
 
M

Marcel Müller

i got an error "A lambda expression with a statement body cannot be
converted to an expression tree" when i write below code.

data = is the iquerable object.

how to solve tis error.

C# does not support arbitrary anonymous functions in LINQ expressions.
You need to define a (private) named function with your function body.

bool ItemPredicate(TypeOfItem item)
{ object oValue =
...
}

data.Where(ItemPredicate)
...


Marcel
 
N

Nick Hounsome

hi,

i got an error "A lambda expression with a statement body cannot be
converted to an expression tree" when i write below code.

data = is the iquerable object.

how to solve tis error.

data.Where(
                        item =>
                        {

An IQueryable is evaluated in the server and hence can only contain
stuff that maps to things the server can understand (typicaly it must
map to SQL)
You can use your lambda if you first bring the data client side with
ToList() and then use Where() on the resulting IEnumerable.
This overload of WHere takes a delegate rather than an expression
tree.
 

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