Inspired by the MVC storefront the latest project I’m working on is using extension methods on IQueryable to filter results.
I have this interface;
IPrimaryKey { int ID { get; } }
and I have this extension method
public static IPrimaryKey GetByID(this IQueryable<IPrimaryKey> source, int id) { return source(obj => obj.ID == id); }
Let’s say I have a class, SimpleObj which implements IPrimaryKey. When I have an IQueryable of SimpleObj the GetByID method doesn’t exist, unless I explicitally cast as an IQueryable of IPrimaryKey, which is less than ideal.
Am I missing something here?
It works, when done right. cfeduke’s solution works. However, you don’t have to make the
IPrimaryKeyinterface generic, in fact, you don’t have to change your original definition at all: