我现在拥有的:
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();
但是,我无法看到第一个语句用于将结果投影到的(匿名)类型的字段。
我是否需要在查询中使用已定义的类型进行投影?有没有办法用匿名类型做到这一点?
[更新]
我设法稍微更改了源查询,在组内部和组之前移动组。这样就可以投射匿名类型的字段,在其他语句中“公开”。
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();
答案 0 :(得分:0)
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项的子集,并且(实际上)将所有那些小的枚举联合回一个大的一。
如果您想选择至少有一个与PaymentNet相关的匿名组的组&gt; 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
对象,这些对象包含你的匿名类型的枚举 - 而不是那种类型的序列本身。