I have a class implementing an interface, and I need to return a Queryable<> list of that interface, given a certain Where predicate :
public interface ISomeInterface
{
int ID{get;}
IQueryable<ISomeInterface> GetWhere(Func<ISomeInterface,bool> predicate);
}
public class SomeClass : ISomeInterface
{
public static IQueryable<SomeClass> AVeryBigList;
public int ID {get;set;}
public IQueryable<ISomeInterface> GetWhere(Func<ISomeInterface,bool> predicate)
{
return (from m in AVeryBigList select m).Where(predicate);
}
}
problem is , this won't even compile, as the predicate won't match.
I've attempted so far:
return (from m in AVeryBigList select m as ISomeInterface)
.Where(predicate);
This will compile, but will fail at runtime, saying that it can't instantiate an interface
Second attempt:
return (from m in AVeryBigList select m)
.Cast<ISomeInterface>
.Where(predicate);
This will fail with a more enigmatic error: Unable to cast object of type 'System.Linq.Expressions.MethodCallExpression' to type 'SubSonic.Linq.Structure.ProjectionExpression'.
Edit:
The answer from wcoenen works as it should. Problem now appears when my AVeryBigList is provided by SubSonic 3.0. I get an exception thrown from within SubSonic when executing a query with Cast<>:
Unable to cast object of type 'System.Linq.Expressions.MethodCallExpression' to type 'SubSonic.Linq.Structure.ProjectionExpression'.
SubSonic.Linq.Structure.DbQueryProvider.Translate(Expression expression) at SubSonic.Core\Linq\Structure\DbQueryProvider.cs:line 203
Should I understand that SubSonic's Linq does not support Cast<> or is this a bug in SubSonic?