I'm fairly new to generics so I wonder if somebody can explain the following problem I'm having. In virtually all my controllers in an ASP.NET MVC application I need to return a filtered list (to populate a JqGrid as it happens where the user will have specified certain filtering criteria). Each controllers list method will return a different IQueryable list so I set about creating a generic method to handle this.
While I was creating my method I defined it in a specific controller. Everything compiled and I got the results I expected. Because I want to call this method from all my controllers I assumed I could simply create another static class, put the method in there and then call that method from all my controllers. But if I try to move the method to anywhere else other than the controller thats calling it, the compiler complains about the last line of the method with the following error:
The type arguments for method System.Linq.Queryable.Where<TSource>(System.Linq.IQueryable<TSource>,
System.Linq.Expressions.Expression<System.Func<TSource,bool>>)
cannot be inferred from the usage.
Try specifying the type arguments explicitly.
public static IQueryable<T> FilteredList<T>(IQueryable<T> list, string filters)
{
var qb = new QueryBuilder<T>();
var whereClause = qb.BuildWhereClause(filters);
return list.Where(whereClause);
}
I've tried list<T>.Where(whereClause)
and list.Where<T>(whereClause)
and just about every other combination, can anyone explain to me where I'm going wrong.