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

  • Thread starter Thread starter Ramkrishnan .
  • Start date Start date
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();
}
}
);
 
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
 
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.
 
Back
Top