简体   繁体   中英

EF Core 6 - TPH Querying with navigation properties

I'm trying to make a poylmorphic query with different navigation properties based on the type of a TPH hierarchy.

Here is my EF Core query:

var composition = await _context.FundCompositions
            .Where(compo => compo.FundId == fundId && compo.Date == datetime)
            .Select(compo => new CompositionDto
            {
                FundId = compo.FundId,
                Date = compo.Date,
                FundName = compo.Fund.Name,
                Cash = compo.CompositionItems
                    .Where(item => item.Asset is Cash)
                    .Select(item => new CashDto
                    {
                        Id = item.AssetId,
                        Amount = item.Amount,
                        Currency = item.Asset.Currency,
                        Name = item.Asset.Name,
                        Quantity = item.Quantity,
                        Country = item.Asset.SecurityType,
                        Region = item.Asset.SecurityType,
                        GicsSector = item.Asset.SecurityType,
                        SecurityType = item.Asset.SecurityType
                    }),
                Basket = compo.CompositionItems
                    .Where(item => item.Asset is Basket)
                    .Select(item => new BasketDto
                    {
                        Id = item.AssetId,
                        Amount = item.Amount,
                        Currency = item.Asset.Currency,
                        Name = item.Asset.Name,
                        Quantity = item.Quantity,
                        Country = item.Asset.SecurityType,
                        Region = item.Asset.SecurityType,
                        GicsSector = item.Asset.SecurityType,
                        SecurityType = item.Asset.SecurityType
                    }),
                Equities = compo.CompositionItems
                    .Where(item => item.Asset is Equity)
                    .Select(item => new EquityDto
                    {
                        Id = item.AssetId,
                        Amount = item.Amount,
                        Currency = item.Asset.Currency,
                        Name = item.Asset.Name,
                        Quantity = item.Quantity,
                        Country = item.Asset.Country.Name,
                        Region = item.Asset.Country.Region.Name,
                        MarketClassification = item.Asset.Country.MarketClassification,
                        SecurityType = item.Asset.SecurityType,
                        GicsSector = ((Equity)item.Asset).GicsSector,
                        Timeserie = ((Equity)item.Asset).Timeserie
                            .Where(ts => ts.Date <= datetime && ts.Date >= datetime.AddDays(-5))
                            .OrderByDescending(ts => ts.Date)
                            .Select(ts => new TimeserieDto(ts.Date, ts.MarketCap))
                            .FirstOrDefault()
                    })
            }).FirstOrDefaultAsync(ct);

As you can see I first constrain the type of my CompositionItems in the Where clause and then Select the properties I need for each derivedType.

I feel like this is tedious, alot of code is repeated and not the correct way to deal with TPH hierarchies. Is there a better way to query derived properties?

Thanks for your help

Apparently, this is already the best way to do it. Good to know anyway.

Thanks for your help Svyatoslav Danyliv!

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