Suppose I have the following entities
public abstract class Animal { public int Id {get;set;} } public class Cat : Animal { } public class Dog : Animal { }
Is it possible to determine the type of an object without creating an instance.
var id = 1; var type = context.Animals.GetTypeOfAnimal(id) public static Type GetTypeOfAnimal(this ObjectSet<Animal> source, int id) {
In one solution, I thought about using the following method ...
public static Type GetTypeOfAnimal(this ObjectSet<Animal> source, int id) { var info = source.Where(a => a.Id == id).Select(a => new {IsDog = a is Dog, IsCat = a is Cat}).First(); if(info.IsDog) return typeof(Dog); if(info.IdCat) return typeof(Cat); return null; }
Rohan west
source share