Tip 5 – How to restrict the types returned from an EF Query.

Imagine you have a model that looks like this:



How do you query for just Cars?

This is where OfType<SubType>() comes in. You write something like this:

var onlyCars = from car in ctx.Vehicles.OfType<Car>()
               select car;

And this works, fine. It restricts the results to just Cars, which incidentally includes Cars, SportsCars and SUVs.

How do you query for just Cars without Sub-Types?

Imagine however your budget only stretches to a simple family car. This means you don't want SUVs and SportsCars.

Now to avoid pulling back all sub-types you need to be very explicit in your query:

var onlyCars = from car in ctx.Vehicles.OfType<Car>()
               where !(car is SportsCar) && !(car is SUV)
               select car;

And now you have something that brings back just the Cars.

Extra Credit

The only unfortunate thing about this solution is that you have to explicitly call out all the possible subtypes you don't want, which in the case of a deep or wide hierarchy might be a lot of types.

It would be better if this was supported (which unfortunately it isn't):

var onlyCars = from car in ctx.Vehicles.OfType<Car>()
               where car.GetType() == typeof(Car)
               select car;

The question is how important do you think it is for us to support this kind of query?

Comments (9)

  1. Thank you for submitting this cool story – Trackback from DotNetShoutout

  2. decarufe says:

    Would it be possible to have something like:

    var onlyCars = from car in ctx.Vehicles.OfType<Car>(true)

                  select car;

    where OfType<T> signature looks like this

    public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source, bool strict)

    where if strict is true then OfType will return only matching types.

  3. Alex D James says:

    It is an interesting idea.

    The only problem is OfType is a standard LINQ operator so doing it like that would be a much wider change than just EF.

    LINQ to objects already supports the syntax I asked about so it is hard to see a new version of OfType being a high enough priority.


  4. onemenny says:

    you should support it

    it’s very intuitive

  5. Yes and no Alex. Indeed, if we do:


    we use the ObjectQuery.OfType method.

    So we can imagine to have a special ObjectQuery definition.

    Today, we can use Include method even if it isn’t a LINQ operator.


  6. Alex D James says:

    @OneMenny thanks for the comment, good to know.

    @Matthieu well the problem is that everytime we create a new method (like Include) that is not a standard LINQ operator, we move further away from something that can be easily mocked with something like


    So personally I’d rather not go further down that track.

    Indeed seeing as this is supporting in LINQ to Objects already using e.GetType() == typeof(requiredType) it makes sense to just support the translation for that.



  7. Meta-Me says:

    Hopefully if you’re reading this you’ve noticed that I’ve started a series of Tips recently. The Tips

  8. zhirani says:

    Yeah we need that support. Currently I have to either rely on builder methods or esql where this is already support like

    select value c

    from container.cars

    where OfType(Entityset,only car)

    something like that in esql

    Zeeshan Hirani

Skip to main content