I am attempting to build a simple Where clause.
This is the code that does not work:
EDIT this code works fine now (thanks to answers below).
public class Item
{
public int Value { get; set; }
public string Name { get; set; }
}
var _List = new List<Item>
{
new Item{ Name = "Smith", Value = 1},
new Item{ Name = "Smith", Value = 2},
new Item{ Name = "Wesson", Value = 3},
new Item{ Name = "Wesson", Value = 4},
};
// Where(x => x.Value == 1)
var _Type = typeof(Item);
var _Prop = _Type.GetProperty("Value");
var _Param = Expression.Parameter(_Type, _Prop.Name);
var _Left = Expression.PropertyOrField(_Param, _Prop.Name);
var _Right = Expression.Constant(1, _Prop.PropertyType);
var _Body = Expression.Equal(_Left, _Right);
var _Where = Expression.Lambda<Func<Item, bool>>(_Body, _Param);
var _Result = _List.AsQueryable().Where(_Where);
Thank you.
There are several problems with your code:
You need to pass
1and not"1"for the integer constant1.Expression.Equals if two expression trees are equal. It returns a
bool.Expression.Equal returns an expression tree that represents an equality check.
The parameter is of type
Itemand notint.The List<T> implements IEnumerable<T>, but not IQueryable<T>.
IEnumerable<T> works with delegates, while IQueryable<T> works with expression trees.
So you need to either compile your expression tree to a delegate
or convert the list to IQueryable<T>.
Working code: