0

I'm trying to do something that would seem very simple. I want to query for all types in a table per hierarchy, except for one type. This has to run as a single (pageable) database query.

To return a single type, I can call IQueryable<T>.OfType<T>(). I don't see any way to pass a subset of types. As far as I know, the Discriminator cannot be exposed as a model property, so I cannot filter with any kind of Where(x => x.Discriminator != value) call.

Someone suggested something like IQueryable<T>.Where(x => x is Type1 || x is Type2 || x is Type3), but I recall that kind of structure not working right. Someone also suggested using OfType for each type, joined by Union calls, but I think that would produce a poorer performing query than necessary when I'm trying to filter a single type from many types (e.g. exclude only 1 of 20 types, using 18 unions).

Is there a known way to perform such a query that I'm missing?

Triynko
  • 18,766
  • 21
  • 107
  • 173
  • I really don't want to have to rely on something like adding an 'Exclude' property to the base table type and flagging it false for everything but the one type I want to exclude. It's an inflexible approach (e.g. what if I need to exclude one type in one context, but two other types in another context... I'd need yet another dedicated property). – Triynko Jan 22 '18 at 04:43
  • You see [this](https://stackoverflow.com/questions/7142214/can-i-specify-a-discriminator-column-with-a-table-per-type-mapping) and [this](https://www.interworks.com/blog/banderton/2011/04/15/querying-specifc-inheritance-types-entity-framework)? – Steve Greene Jan 22 '18 at 15:43

0 Answers0