Entity Framework TPT Inetitance Subtypes
How to determine the subtype of an object with TPT-Inhertiance?
If I have a base class Person and two subclasses Manager and Customer, it should be able to query all persons and then group their subclasses using the GetType method, but the return type is always human. For example:.
var persons = ctx.Persons.ToList();
var managers = persons.Where(x => x.GetType() == typeof(Manager)).ToList();
+3
source to share
1 answer
Choose:
var managers = ctx.Persons.OfType<Manager>().ToList();
also useful if you don't know what you have
var persons = ctx.Persons.ToList();
Type modelType = persons.First().GetType();
if (modelType.BaseType == typeof(Manager))
{
((Manager)persons.First()).GiveNeilAPayRise = true;
}
+2
source to share