case: i have a list of Companies, each Company contains one Store and some other properties like address, name, .... A store can be of type FruitStore or of type VegetableStore (both are derived from Store). If the store is a fruitstore i want to include a property that is unique for fruitstore for example TheBestOrange (wich is an object).
how do i include theBestOrange in my query and also the properties from company?
I have
companies.Include(s => s.Store)
.OfType<FruitStore>()
.Include(a => a.theBestOrange)
but this changes my return type to a list of fruitstores instead of a list of companies
i hope i made myself clear.
I'm assuming you want to include all stores in your query, not just fruit stores. You can't project to a list of objects where some of the objects include a property and others don't, since they would be different types. So it will have to be a nullable property. Maybe something like this (untested):
var query = companies.Select(c => new {
CompanyProp1 = c.CompanyProp1,
CompanyProp2 = c.CompanyProp2,
StoreProp1 = c.Store.StoreProp1,
TheBestOrange = c.Store is FruitStore ? ((FruitStore)c.Store).theBestOrange : null
});
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.