如果我有以下查询: -
var Values = from data in DtSet.Tables["tblCosts"].AsEnumerable()
group data by new
{
InvNo = data.Field<double>("InvoiceNo"),
AccRef = data.Field<double>("SiteRefNum"),
}
into g
select new
{
Code = "1",
InvType = "I",
Account = g.Key.AccRef,
InvNo = g.Key.InvNo,
ChargeTotal = g.Sum(d => d.field<double>("Charge")
};
...排除列ChargeTotal总和为零的行的最佳方法是什么?我假设我必须使用WHERE
,但我不确定在哪里放置它。
答案 0 :(得分:5)
您没有很多解决方案,因为必须在where
之后调用group by
(它取决于组),然后:
var Values = from data in DtSet.Tables["tblCosts"].AsEnumerable()
group data by new
{
InvNo = data.Field<double>("InvoiceNo"),
AccRef = data.Field<double>("SiteRefNum"),
}
into g
let sum = g.Sum(d => d.field<double>("Charge")) // Sum only computed once
where sum != 0
select new
{
Code = "1",
InvType = "I",
Account = g.Key.AccRef,
InvNo = g.Key.InvNo,
ChargeTotal = sum
};
要略微改善性能(你必须测试两者以确定是否有任何性能改进,因为它高度依赖于数据),如果d.field(“Charge”)总是> = 0,你可以这样做:
var Values = from data in DtSet.Tables["tblCosts"].AsEnumerable()
group data by new
{
InvNo = data.Field<double>("InvoiceNo"),
AccRef = data.Field<double>("SiteRefNum"),
}
into g
where g.Any(d => d.field<double>("Charge") != 0)
select new
{
Code = "1",
InvType = "I",
Account = g.Key.AccRef,
InvNo = g.Key.InvNo,
ChargeTotal = g.Sum(d => d.field<double>("Charge"))
};