Let’s say I have two Tables, Lunch and Dinner. I know that both contain the DateTime property “Time”.
If I have a generic method GetTime, how could I return db.Lunch.Time when T is Lunch and db.Dinner.Time when T is Dinner? I’m trying to achieve this without testing for T individually using typeof, but rather generically.
Pseudocode:
public T GetTime<T>(DateTime dt)
{
return MyDataContext.GetTable<T>().Where(entity => entity.Time == dt);
}
So when I call GetTime<Dinner> it will automatically look in the Dinner Table for all dinner entities with the property time equal to my supplied parameter dt.
The problem is that I can’t specifiy entity.Time in my expression because T is a generic. My question is how to get around that, so that I can look for any T (knowing that all my entities in fact have the Time property) without having to create specific methods for Dinner and Lunch.
You’d have to have both classes implement an interface something like this:
And then in your generic method: