简体   繁体   中英

Entity Framework ThenInclude doesn't include properties of the grandchild

Entity Framework ThenInclude doesn't include any properties of the grandchild. When I try to access the property "Total" of the "GrandChild" I get an error "CS1061 'ICollection' does not contain a definition for 'Total' and no accessible extension method 'Total'". The intellosense doesn't work with "GrandChild" at all. It works fine with the "Child", but not with the "GrandChild". When I make the request from Postman I can see that Entity includes "GrandChild" and it has property "Total" for sure (and all other properties).

var products = dbf.Products
                .Include(a=>a.Child)
                .ThenInclude(b => b.GrandChild)          
                .Where(c => c.ProId >72200);

            foreach(var p in products)
            {
               p.Child.GrandChild.Total// I get error here

            }

Try this for access all total properties of GrandChild:

var totals = p.Child.GrandChild.Select(gc=> gc.Total);

But if you want get total grandchilds (count of grandchilds) you should try this:

var count = p.Child.GrandChild.Count();

The error is quite clear, or?

GrandChild is an ICollection (which we could see if you would provide the relevant class definitions). There is no Total function on an ICollection. Period. Never was. This is not an Entity Framewor kquestion at all - you bascially assume that ThenInclude will magically provide a proeprty Total on an ICollection.

How is this supposed to work, given that Total requires to know how to add and unless the element in the collection is a scalar of any type that is simply not possible.

Use logic to calculate the total.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM