繁体   English   中英

Linq集团通过不采取内部实体

[英]Linq Group By not taking inner entity

我正在使用实体框架并通过表格进行分组。 我的查询如下: -

var brokerPaymentLists = dbContext.BrokerPayments
    .Include("PaymentDetail")
    .Where(bp => bp.IdPaymentStatus == (long)EntityModel.Additions.Variables.PaymentStatus.ALLOTED)
    .GroupBy(bp => bp.IdBroker,
    (key, g) => new
    {
        IdBroker = key.Value,
        BrokerPayments = g.ToList()
    }).ToList();

我已经包含了PaymentDetail但在分组之后,我可以看到BrokerPayments中每个项目的paymentdetail为null。 任何建议为什么会这样,我怎么能这样做,我可以我的paymentDetail insisde每个BrokerPayments;

使用Include急切加载需要在应用Include不更改数据的形状。 在您的情况下,这意味着查询必须返回IQueryable<BrokerPayments> GroupBy运算符更改形状,因为它返回IQueryable<IGrouping<TKey, TSource>> 投影和自定义连接也会发生相同的情况。

作为一种解决方法,您可以在LINQ to Objects中执行分组,例如:

var brokerPaymentLists = dbContext.BrokerPayments
    .Include("PaymentDetail")
    .Where(bp => bp.IdPaymentStatus == (long)EntityModel.Additions.Variables.PaymentStatus.ALLOTED)
    .AsEnumerable()
    .GroupBy(bp => bp.IdBroker,
    (key, g) => new
    {
        IdBroker = key.Value,
        BrokerPayments = g
    });

注意:请注意查询exectuion不会被删除

暂无
暂无

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

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