Search code examples
c#linqentity-framework-4parenttable-per-type

Checking Parent Type TPT EF4


I have a TPT scenario in EF4 with an abstract base class.

I need to wite a linq query against a collection of children types to get the value from a field of one type of parent.

eg.

ThisChild = Children.Where(c. => c.Parent.OfType<Mother>.JewelleryCollection > 10).FirstOrDefault();

In this case, Parent is the abstract class with Mother being a type of Parent. Mother is the only type that has a JewelleryCollection field.

The example above breaks because you cannot use the .OfType<> method. How can I best structure this query?

Thanks.


Solution

  • Because you are running the query on ObservableCollection it is Linq-to-objects where you can simply use conversion:

    ThisChild = Children.FirstOrDefault(c => 
        (c.Parent is Mother) && (((Mother)c.Parent).JewelleryCollection > 10));