LINQ Obscura: outer join a base class table to a subclass table

By May 8, 2014News

I’ve gone on at length before about inheritance in EF, specifically Table Per Type inheritance, and for the sake of this post, I’m going to assume you already know how to perform an outer join with LINQ. But what if you want to perform an outer join of a base class table to one (or more) subclass tables? I couldn’t think of a way to get it done, but a colleague presented a solution that seems blindingly obvious once demonstrated. In the following incantation, assume that Student is modeled as a subclass of Person:

The key is simply to realize that Persons represents the base table in the DB and Persons.OfType<Student>() represents the subclass table, and the rest falls into place.

Did you like this post? Then you would love FalafelCon! Sign up and let us know you’re coming on Twitter!

The following two tabs change content below.

Adam Anderson

Master Consultant at Falafel Software
Adam Anderson is a Microsoft Certified Solution Developer with over 20 years of experience. He started as a consultant using Delphi to rapidly deliver custom, high-quality database applications in a wide variety of industries. Over the course of his career, he has acquired a wide variety of skills including database design, query and index optimization; Database metaprogramming and databases with dynamic, self-modifying schemas; reporting with Crystal Reports, SQL Reporting Services, and Telerik Reporting; .NET Framework, ASP.NET MVC and Web API; ServiceStack; LINQ and Entity Framework; HTML, CSS, and Javascript; Kendo UI; Regular expressions; and lately, Node.js, Express, Angular, MongoDB, PostgreSQL, Redis, Windows Azure, AWS, and more. Fortunately, Adam likes learning new things and then writing about them!