Using the Entity Framework LINQ, I would like to return a parent class for each parent table and populate a property whose type is an interface in the parent class with one of several specific interface implementations. What specific implementation should be determined during the query based on the value of the field in the parent table.
In a very simplified example, I have 3 tables and 3 corresponding POCOs.
Simple tables for example

Simple classes for an example.
internal interface IConfiguration
{
}
internal class ConfigurationContainer
{
public IConfiguration Config { get; set; }
}
internal class ConfigurationSouth : IConfiguration
{
}
internal class ConfigurationNorth : IConfiguration
{
}
Unfortunately, I go through all the parent results and determine which subquery to use during the loop. Something like this block.
foreach (var configMaster in db.ConfigMasters.ToList())
{
var configContainer = new ConfigurationContainer();
if (configMaster.IsNorth)
configContainer.Config = (from x in db.ConfigNorths
select new ConfigurationNorth())
.FirstOrDefault();
else
configContainer.Config = (from x in db.ConfigSouths
select new ConfigurationSouth())
.FirstOrDefault();
}
. , EF LINQ , POCOs.
LINQ, , Config . , .
using (var db = new Entities())
{
var qry = from cfgMaster in db.ConfigMasters
let configNorth = (from x in db.ConfigNorths
select new ConfigurationNorth())
.FirstOrDefault()
let configSouth = (from x in db.ConfigSouths
select new ConfigurationSouth())
.FirstOrDefault()
select new ConfigurationContainer()
{
Config = cfgMaster.IsNorth ? configNorth : (IConfiguration) configSouth
};
var results = qry.ToList();
}
Exception
: System.NotSupportedException: "EFTest.ConfigurationNorth", "EFTest.IConfiguration". LINQ to Entities Entity Data Model.