Group by entity type RRS feed

  • Question

  • Is it possible to group by entity type in a Linq to Entity query ?
    My entities are TPT (one table per type), so I don't have a column in the entity that tell me its type.

    Something like this (GetType() isnt recognized by the EF query provider):
    var query = from o in context.Orders
    group o by o.GetType() into g
    select new
    Type = g.Key,
    Orders = g.AsEnumerable(),

    I know I could execute the groupby in Linq To Object with a AsEnumerable() on context.Orders.AsEnumerable()
    But I think I should be able to do the grouping on the server.
    Monday, March 22, 2010 6:46 PM


  • Hello

    I don't think it is supported to embed the GetType() in LINQ to Entities queries.  Besides, for TPT inheritance, how about directly calling OfType() method to retrieve the certain type entities?  

    I would also recommend you make it as a LINQ to Objects and group the data at the client side. 

    Best Regards,
    Lingzhi Sun

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, March 29, 2010 9:06 AM