Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8626693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:05:46+00:00 2026-06-12T08:05:46+00:00

Using Entity Framework LINQ, I’d like to return a parent class for every parent

  • 0

Using Entity Framework LINQ, I’d like to return a parent class for every parent table and populate a property, whose type is an interface, on the parent class with one of multiple concrete implementations of an interface. Which concrete implementation should be determined at query time based on the value of a field in the parent table.

In a very simplified example, I have 3 tables and 3 corresponding POCOs.

Simple tables for the sake of example

Three Tables: Master and two children

Simple classes for the sake of example

internal interface IConfiguration
{
}

internal class ConfigurationContainer
{
    public IConfiguration Config { get; set; }
}

internal class ConfigurationSouth : IConfiguration
{
}

internal class ConfigurationNorth : IConfiguration
{
}

Unfortunately, I loop through all the parent results and determine which subquery to use while inside 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();
}

Looping through each of the parent records to perform a subquery is less than optimal. I’d really like the EF LINQ to perform the query in one trip to the database as well as do the projections into my POCOs.

I came up with this LINQ to query the database in one trip and return the projected objects such that the Config property would be populated by one of the two subqueries at querytime. Though it compiles, it throws an exception at runtime.

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

Unhandled Exception: System.NotSupportedException: Unable to cast the
type ‘EFTest.ConfigurationNorth’ to type ‘EFTest.IConfiguration’. LINQ
to Entities only supports casting Entity Data Model primitive types.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:05:48+00:00Added an answer on June 12, 2026 at 8:05 am

    Entity Framework does not (yet?) support the use of interfaces this way, basically because the interface is not (and can not be) registered with the model. What you want is only possible when you let both classes derive from a base class that is part of the model too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using LINQ Entity framework. I have a SQL table and I want
Hi i am using entity framework and LinQ. I have a table objects called
I am using Entity Framework. I am trying to get the results from linq
I am using Linq and the Entity Framework. I have a Page Entity, that
I'm not using LINQ-to-SQL or Entity Framework bits in a web app, and have
I recently switched from using Linq to Sql to the Entity Framework. One of
I have a database and I am using the Entity Framework with Linq 2
Hi I'm developing a winform application using C# and the entity framework (linq to
Supposedly, microsoft says that we should all be using Entity Framework, using LINQ (to
I'm having trouble building an Entity Framework LINQ query whose select clause contains method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.