【发布时间】: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