linq聚合
假设我有这样的课程:
public class ServiceCall
{
public int ServiceCallID {get; set;}
public DateTime ReportedTimestamp {get; set;}
public bool IsPaid {get; set;}
public decimal LabourNet { get; set;}
public decimal LabourVat {get; set;}
}
public class UsedPart
{
public int UsedPartID { get; set; }
public decimal NetPrice { get; set; }
public decimal VatAmount {get; set;}
}
我想返回以下格式的数据:
Month #CallsReceived AmountInvoiced MoneyReceived
Jan 2009 202 €32050.20 €29200.00
Feb 2009 213 €35050.20 €34200.00
发票金额是一个月内所有 (NetPrices + VatAmounts) + (LabourNet + LabourVat) net a 的总和,收到的钱是所有 IsPaid = 的总和真的。 我无法正确获取此报告的 linq 查询。我不确定在哪里进行分组以及何时执行求和。
我目前的 linq 查询如下,但目前我没有得到任何接近我想要的结果。有人有一些 linqy 善良吗?
var q = from s in ServiceCalls
join up in UsedParts on s.ServiceCallID equals up.ServiceCallID into tmp
from nullablePart in tmp.DefaultIfEmpty()
group s by s.ReportedTimestamp.Month
into grp
select new
{
Month = grp.Key,
CallsReceived = grp.Count(),
AmountInvoiced = grp.Sum(s => s.UsedParts.Sum(p => p.NetPrice)),
};
Say I have classes like:
public class ServiceCall
{
public int ServiceCallID {get; set;}
public DateTime ReportedTimestamp {get; set;}
public bool IsPaid {get; set;}
public decimal LabourNet { get; set;}
public decimal LabourVat {get; set;}
}
public class UsedPart
{
public int UsedPartID { get; set; }
public decimal NetPrice { get; set; }
public decimal VatAmount {get; set;}
}
I want to return data in for format:
Month #CallsReceived AmountInvoiced MoneyReceived
Jan 2009 202 €32050.20 €29200.00
Feb 2009 213 €35050.20 €34200.00
Amount invoiced is a total of all the (NetPrices + VatAmounts) + (LabourNet + LabourVat) net a for a month and money received is a total of all where IsPaid = true.
I'm having trouble getting the linq query correct for this report. I'm not sure where to have my grouping and when to perform the Sum.
The linq query that I have at the moment is as follows, but I'm not getting anything near what I want at the moment. Anyone with some linqy goodness?
var q = from s in ServiceCalls
join up in UsedParts on s.ServiceCallID equals up.ServiceCallID into tmp
from nullablePart in tmp.DefaultIfEmpty()
group s by s.ReportedTimestamp.Month
into grp
select new
{
Month = grp.Key,
CallsReceived = grp.Count(),
AmountInvoiced = grp.Sum(s => s.UsedParts.Sum(p => p.NetPrice)),
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我必须将其分解为几个子查询才能使其正常工作。很可能有更好、更有效的方法来做到这一点,但我目前有一个 linq 解决方案。不包括获取收到款项的代码,因为它与其他代码非常相似,除了 IsPaid = true 上的过滤之外。
由于我使用的是 LLBL Gen Pro,因此在我的环境中略有不同,但您可以从此代码中获取基本要点:
OK, I've had to break this down into a few sub queries to get it working. There very may well be a better and more efficient way of doing this, but I have a linq solution for the moment. Not including the code for getting the Money Received as it's very similar to the other code except filtering on IsPaid = true.
It's slightly different in my environment as I'm using LLBL Gen Pro, but you can get the basic gist from this code:
我知道这个问题现在已经成为一个古老的历史,但这是我的答案。
享受!
I know this question is a ancient history now, but here's my answer.
Enjoy!