I am working on this mvc project following Rob Connery’s storefront video series and applying the techniques.
On the filtering and extensions methods, i started repeating myself a lot such as:
public static Sponsor WithID(this IQueryable<Sponsor>qry, int ID)
{
return qry.SingleOrDefault(s => s.ID== ID);
}
public static Keyword WithID(this IQueryable<Keyword>qry,int ID)
{
return qry.SingleOrDefault(s => s.ID== ID);
}
....
To prevent this,I try to write a generic extension like this:
public static T WithID<T>(this IQueryable<T>qry,int ID)
{
return qry.SingleOrDefault(s=>ID==ID);
}
however s does not have ID, so how would you solve this?
You’ll need an interface which declares an ID property, e.g.
Then you can write:
Of course, you’ll have to make all the appropriate classes implement the interface – but that’s usually easy; if the classes are designer-generated you probably want to use partial classes.