I am new to generics, so I wonder if anyone can explain the following problem that I have. In almost all of my controllers in an ASP.NET MVC application, I need to return a filtered list (to populate the JqGrid, as it happens when the user specifies certain filtering criteria). Each controller list method returns a different IQueryable list, so I decided to create a generic method for this.
While I was creating my method, I defined it in a specific controller. Everything is compiled, and I got the expected results. Since I want to call this method from all my controllers, I assumed that I could just create another static class, put a method there and then call this method from all my controllers. But if I try to move the method to a place other than the controller that calls it, the compiler complains about the last line of the method with the following error:
Type arguments for a method System.Linq.Queryable.Where<TSource>(System.Linq.IQueryable<TSource>,
System.Linq.Expressions.Expression<System.Func<TSource,bool>>)
cannot be taken out of use. Try explicitly specifying type arguments.
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 tried list<T>.Where(whereClause)and list.Where<T>(whereClause)almost any other combination, can someone explain to me where I am wrong.