Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework OfType()

Let's assume I have a parent entity "Firm" and a handful of child entities that inherit from Firm. Assuming the children are named "RedFirm", "GreenFirm", and "BlueFirm", what is the proper way to query the Firm collection if I want to retrieve only RedFirm and GreenFirm instances?

I know I can do context.Firms.OfType(RedFirm), but that only returns the RedFirm instances. Is there anyway to pass a collection of types into OfType or something similar to that? I suppose this can be done through a union but I would think that would be less efficient.

like image 645
e36M3 Avatar asked Nov 03 '10 17:11

e36M3


2 Answers

context.Firms.Where(x => x is RedFirm || x is GreenFirm);
like image 185
Kirk Woll Avatar answered Sep 25 '22 15:09

Kirk Woll


You could do something like:

context.Firms.Where(item => (!(item is BlueFirm)));
like image 27
Erik Noren Avatar answered Sep 22 '22 15:09

Erik Noren