e36M3 e36M3 - 1 year ago 96
C# Question

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

, 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.

Answer Source
context.Firms.Where(x => x is RedFirm || x is GreenFirm);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download