简体   繁体   English

EF Core 6 - 使用导航属性进行 TPH 查询

[英]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.我正在尝试根据 TPH 层次结构的类型使用不同的导航属性进行多态查询。

Here is my EF Core query:这是我的 EF Core 查询:

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.如您所见,我首先在 Where 子句中限制我的 CompositionItems 的类型,然后在 Select 中限制每个派生类型所需的属性。

I feel like this is tedious, alot of code is repeated and not the correct way to deal with TPH hierarchies.我觉得这很乏味,重复了很多代码,而不是处理 TPH 层次结构的正确方法。 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!感谢您的帮助斯维亚托斯拉夫丹尼利夫!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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