I have a function that gets a filtered list of items based on the lambda expression I pass in. Below is an example of what I am doing. The List is an ObservableCollection of myBase and the filter I am passing in would be something like this: t => t.Data.Any()
At the moment if I replace "filter" with the above lambda it works but when I pass it in and use the local variable, filter, I get a compile error like “cannot be inferred from the usage. Try specifying the type arguments explicitly.”</p>
protected IEnumerable<myBase> GetByFilter(Expression<Func<myBase, bool>> filter)
{
IEnumerable<myBase> itemlList = _items.Where(filter).ToList();
return itemlList ;
}
What am I missing here?
Edit -------------------
I am trying to get a subset of the original list based on the lambda passed in. I think I may be able to get away with the lambda line returning another observableCollection rather than an IEnumerable one, if that is possible?
Edit -------------------
With help from Ruslan, I have fixed my problem. My code now compiles and looks like this:
protected IEnumerable<myBase> GetByFilter(Func<myBase, bool> filter)
{
IEnumerable<myBase> itemlList = _items.Where(filter).ToList();
return itemlList ;
}
I can pass in a filter like "t => t.Data.Any()" and get all the items etc. I just needed to drop "Expression" from the filter parameter.