簡體   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