How do I request an Entity Framework object container for derived class objects?

I have a complex inheritance structure in my data model. Most of the classes in my model (which was created in VS 2010 with the database he created, are based on 3 main classes). And only these 3 classes can be found among members of the context. How to use all derived classes in general?

+3
source share
2 answers

You can request subtypes as follows:

  var horses = from animal in ctx.AnimalSet
               where animal is Horse
               select animal as Horse;

This will display all of the Horse objects from the Animal set in the context of my context.

If you want to request properties of type sub, you can do:

var horses = from animal in ctx.AnimalSet
             where animal is Horse //edit, this line is not needed
             let horse = animal as Horse
             where horse.TracksWon > 3
             select horse;

SQL, , , , .

+6

- , . OfType(). :

var horses = from animal in ctx.AnimalSet
             where animal is Horse
             select animal as Horse;

horses IQueryable<Animal>. , , IQueryable<Horse>, :

var horses = from animal in ctx.AnimalSet.OfType<Horse>()
             select animal;

... :

var horses = ctx.AnimalSet.OfType<Horse>();

, :

var horses = from horse in ctx.AnimalSet.OfType<Horse>()
             where horse.TracksWon > 3
             select horse;

, IQueryable<Horse>.

+10

Source: https://habr.com/ru/post/1744594/


All Articles