【问题标题】:Fields not visible after groupby?groupby 后字段不可见?
【发布时间】:2016-08-03 15:53:57
【问题描述】:

我现在拥有的:

var batch_pymnts2 = (from a in ctx.WarehouseStatementBatchPayments
                     join b in ctx.WarehouseStatementBatches on a.WarehouseStatementBatchID equals b.ID
                     join c in ctx.WarehousePaymentInvoices on a.ID equals c.WarehouseStatementBatchPaymentID
                     where b.ID == batchID
                     select new
                     { 
                         PaymentId = a.ID, 
                         PaymentNet = a.Net, 
                         PaymentType = a.Type 
                     })
                     .GroupBy(d => d.PaymentId).Where(x => x.Count() == 1);

我需要像这样查询这些结果:

var test = (from a in batch_pymnts2 where a.PaymentNet > 100 select a).ToList();

但是,我看不到第一条语句用于将结果投影到的(匿名)类型的字段。

我需要在查询中使用定义的类型来进行投影吗?有没有办法用匿名类型做到这一点?

[更新]
我设法稍微更改了源查询,将 group by 移动到 group by 内部和之前。这使得被投影的匿名类型的字段在进一步的语句中被“公开”。

            var count2 =  (from a in WarehouseStatementBatchPayments
                                                 join b in WarehouseStatementBatches on a.WarehouseStatementBatchID equals b.ID
                                                 join c in WarehousePaymentInvoices on a.ID equals c.WarehouseStatementBatchPaymentID
                                                 group a by a.ID into grp
                                                 from d in grp
                                                 where d.WarehouseStatementBatchID == batchID && grp.Count() == 1
                                                 select new { PaymentId = d.ID, PaymentNet = d.Net, PaymentType = d.Type }).ToList();   

【问题讨论】:

  • 您按PaymentId 分组,您为什么希望看到其他字段?
  • 我想要一个来自 WarehouseStatementBatchPayment 的一些值的列表,但只有那些 WarehouseStatementBatchPayment.ID 在 WarehouseStatementBatchInvoices ONCE 中的值
  • @bitshift 这并没有解决他的问题,也没有解释为什么您试图通过 PaymentNet 在您的问题中进行查询。
  • @bitshift "这让被投影的匿名类型的字段在进一步的语句中被“公开”。"? “暴露”是什么意思?它们藏在某个地方吗?

标签: c# sql-server linq entity-framework-6


【解决方案1】:

batch_pymnts2 是一组对象的序列。实际上,它是您的匿名类型的集合的集合。 batch_pymnts2 中的每个项目都有这个:

group.Key; /* a PaymentId value */

((IEnumerable)group); /* the anon type items grouped together in this group */

Those group objects implement the IGrouping interface。他们的Key 属性是定义组的PaymentId 值。如果您枚举组(它们实现IEnumerable<T>),您将获得按PaymentId 分组的匿名对象:

var test = batch_pymnts2.SelectMany(g => g.Where(anon => anon.PaymentNet > 100));

test 现在是您的匿名类型的枚举,因为我们现在已经枚举了来自每个组的 anon 项目的子集,并且(实际上)将所有这些小的 anon 枚举合并为一个大的枚举。

如果您想选择至少有一个匿名事物且 PaymentNet > 100 的群组,请尝试以下操作:

//  Groups which have at least one PaymentNet > 100
var t2 = batch_pymnts2.Where(g => g.Any(anon => anon.PaymentNet > 100));

//  PaymentIds of the groups which have at least one PaymentNet > 100
var ids = t2.Select(g => g.Key);

//  PaymentIds that appear only once
var singles = t2.Where(g => g.Count == 1).Select(g => g.Key);

我不知道您为什么要对它们进行分组,或者您的 PaymentNet > 100 查询要完成什么,所以我不确定如何编写您想要的查询。但是您的出发点是您要查询包含匿名类型枚举的 group 对象序列,而不是该类型本身的序列。

【讨论】:

  • 很抱歉给您带来了困惑。请参阅我的更新原始帖子。
猜你喜欢
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多