I have a generic class that essentially extends a linq class. This means that I can’t just extend the linq class, I need to have a separate generic class.
public LinqCake // Linq Class from DBML
{
string Name;
FrostingType Type;
}
public class Cake<T> : LinqCake where T is FrostingType
{
public bool SomeProperty
{
get
{
return LinqCake.Type == FrostingType;
}
}
// I basically want to do something like this,
// but I obviously can't because this code isn't valid
public Cake (int ID)
{
this = db.LinqCakes.Where(x=>x.ID = ID).Single();
}
}
public void Main()
{
Cake<Chocolate> ChocolateCake = Cake<Chocolate>(7);
Console.WriteLine(ChocolateCake.SomeProperty);
}
I basically want to populate Cake’s inherited class LinqCake with information from the database. Can I do this with just an implicit cast?
How do I accomplish the spirit of what I’m trying to do? I can think of two ways of sort of doing this; A) Add a private LinqCake member B) Essentially just read in each property of the LinqCake class into duplicate properties of the Cake class.
I feel like there is a better solution, any idea?
If I understand your question your doing a projection here which involves mapping the fields from type
LinqCaketo those of typeCake. You can’t set aLinqCaketo be aCakebecause it isn’t specified like that in your type hierarchy.Maybe a tool like AutoMapper can help you.