none
C# Table Per Hierarchy Projection Union RRS feed

  • Question

  • I'm using table per hierarchy to project to model classes. When I use of type in Linq to Entities, all of my results are always the last type which was projected. What I mean is if I have these for domain models:

    public interface IPerson { string Name {get;set;} }
    public abstract class Person : IPerson { public string Name { get;set; } }
    public class Employee : Person{ }
    public class Manager : Person { }
    And I have a Person table with a Name and type column, and use table per hierarchy to yield me specific Entities (which I'll refer to as EmployeeEntity and ManagerEntity).

    When I use this code:

    IQueryable<IPerson> people = entities.OfType<ManagerEntity>().Select(x => new Manager 
    { Name = x.name }).Concat(entities.OfType<EmployeeEntity>().Select(y => new Employee
    { Name = y.name }));

    var projectedPeople = people.ToList();

    All of the types of objects in projectedPeople are Employee, even the the objects that should be of type Manager. If I remove the Concat call and run them individually, I get the correct type back (Manager or Employee).

    What am I doing wrong here? I wrote some test code that removed Linq from the equation by making dummy lists of objects and calling AsQueryable on them, followed by a projection, and that worked as it should. I've found that
    if I call ToList on the Manager Select (entities.OfType<ManagerEntity>().Select(x => new Manager { Name = x.name })).ToList(), then it initializes the appropriate type of object correctly.
    Thursday, June 6, 2013 12:54 PM

All replies

  • Hi Robert,

    Thanks for your post.

    However I'm not able to get it work using your code:

    IQueryable<IPerson> people = entities.OfType<ManagerEntity>().Select(x => new Manager 
     { Name = x.name }).Concat(entities.OfType<EmployeeEntity>().Select(y => new Employee
     { Name = y.name }));
    

    I always get a compile-time error. According to the definition of the Concat extension method, the two sequences should have the same type(TSource). I think we cannot concatenate two sequences with different types.

    However, you may have a look at this thread about how to concat two different IQueryable<T>:

    http://stackoverflow.com/questions/13331786/how-can-i-concatenate-two-different-listt-where-both-have-same-base-class

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 11, 2013 1:39 AM
    Moderator
  • Hi Chester, 

    Thanks for your reply. I just typed this code up on a whim off my head as I don't want to paste my production code in - I have something similar compiling and running, but always returning 1 type instead of the correct types. It shouldn't be concating 2 different types because they're both being projected to types that implement IPerson. Try adding the type parameter of IPerson on your Concat call - I.E (both Manager and Employee are of type IPerson). I'm starting to wonder if it's a problem with the Entity Framework Queryable provider.

    IQueryable<IPerson> people = entities.OfType<ManagerEntity>().Select(x => new Manager 
     { Name = x.name }).Concat<IPerson>(entities.OfType<EmployeeEntity>().Select(y => new Employee
     { Name = y.name }));
    Friday, June 14, 2013 2:46 AM