none
Really need help with this LINQ Query using inheritance. RRS feed

  • Question

  • Hi All, 

    I have a slight issue with my Linq query. I thought I had gotten to work completely but forgot I was missing how I would be able to filter out a property that has been inherited.

    Let's say I have 2 tables I created in MVC.

    Table 1
    Cars

    //Here we also have two tables "Sedan" and "SUV". Because they inherit Cars MVC mashed them up into one table "Cars".

    Table 2
    Salesman

    My Query is as follows:

        var statsModel =  (
                    from c in db.Cars
                        join s in db.Salesman on c.SalesmanId equals c.ID into sales
                    select new { s, sales }
                    into data
                    group data by new { data.c.ModelName, data.c.Year } into g
                    select new MyData
                    {
                        CarYear = g.Key.Year,
                        SomeCount = g.Count(d => (d.c.Status == "Sold"),
                        // etc.. a few more counts
                        // NOW I need to get all Cars that are SUV and HasPremium and do a Sum the SUV value
                        HelpWithSUVSum = ....
                    }
                ).ToList();

    How Do I do a count from Cars that are of Type SUV that have the property HasPremium == True?

    I tried doing this but got an error "DbOfTypeExpression requires an expression argument with a polymorphic result type that is compatible with the type argument."

    MySum = g.OfType<CarsSUV>().Where(d => d.HasPremium == true && (d.Status == "Sold").Sum(d => d.CarValue)
    

    Thanks for your help.





    • Edited by MVCNewb Tuesday, July 9, 2019 2:42 PM new info
    Monday, July 8, 2019 5:38 PM

All replies

  • Hi,

    I noticed that you want to filter the table "SUV". So, why not query the "SUV" directly?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 9, 2019 9:58 AM
    Moderator
  • I am using TPH. Basically there is no SUV or Sedan table but all in one table Cars. Linq 2 entities.

    Table-per-Hierarchy (TPH)

    The TPH inheritance states that all entities, in a hierarchy of entities, are mapped to a single table in storage schema. It means, there is only one table in database and different Entity types in Entity model that inherits from a base Entity are mapped to that table.


    • Edited by MVCNewb Tuesday, July 9, 2019 12:52 PM new info
    Tuesday, July 9, 2019 12:31 PM
  • Hi,

    About "TPH", here I found an article maybe you can refer to.

    Inheritance with EF Code First: Part 1 – Table per Hierarchy (TPH).

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 10, 2019 9:58 AM
    Moderator