I am trying to use a linq expression to filter a list in a generic method and can’t quite get it right.
Here is what it looks like:
the call:
Expression<Func<SomeObject, object>> screen = i => i.BooleanFlag == false;
SomeObjectList.Build<SomeObject>(
screen,
NonBuiltListOfTypeSomeObject);
}
the method:
public void Build<T>(
Expression<Func<T,object>> screen,
List<T> items) where T : class
{
this.Values = items.Select(f => new ModelForBuild()
{
//Build Parameters
}).ToList();
}
What I am trying to do is screen the items before they are built into this.Values. I pass in the linq expression trying to deny access based on the boolean property in SomeObject. However, the compiler tells me I cannot use
this.Values = items.Where(screen).Select(f => new ModelForBuild()...
I have tried various ways so maybe there is a better way to do this, if so, what is that way OR how can I effectively include the linq expression to screen the list of items?
EDIT
Error message from .Where(screen)
Error 1 Instance argument: cannot convert from 'System.Collections.Generic.List<T>' to 'System.Linq.IQueryable<T>'
Error 2 'System.Collections.Generic.List<T>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where<TSource>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,bool>>)' has some invalid arguments
Error 24 Argument 2: cannot convert from 'System.Linq.Expressions.Expression<System.Func<T,object>>' to 'System.Linq.Expressions.Expression<System.Func<T,bool>>'
I think you mean something like this:
You don’t need to use
Expression<T>.