SiteCode | date |softwarename | Success | Error
ASZ 2017-08-5 a 1 1
ASZ 2017-08-5 b 2 2
ASZ 2017-08-5 a 3 3
ASZ 2017-08-6 a 6 6
NBL 2017-08-5 a 5 5
var CurrentCustomer = Database.Session.Query<Customer>()
.FirstOrDefault(x => x.deleted_at == null
&& x.Customer_Username == User.Identity.Name);
if(CurrentCustomer != null)
{
var CurrentDepValues = Database.Session.Query<deployments>()
.Where(x => x.SiteCode == CurrentCustomer.SiteCode
&& x.softwarename == current_type)
.OrderBy(x => x.date)
.ToList();
}
我有这样的数据库。我想得到如下的输出。相同的日期值应该是总和。
SiteCode | date |softwarename | Success | Error
ASZ 2017-08-5 a 4 4
ASZ 2017-08-6 a 6 6
答案 0 :(得分:1)
使用GroupBy
:
if(CurrentCustomer != null) {
var CurrentDepValues = Database.Session.Query<deployments>()
.Where(x => x.SiteCode == CurrentCustomer.SiteCode
&& x.softwarename == current_type)
.GroupBy (x => new {x.SiteCode, x.date, x.softwarename})
.Select (g => new {
SiteCode = g.Key.SiteCode,
Date = g.Key.date,
SoftwareName = g.Key.softwarename,
Success = g.Sum(x => x.Success),
Error = g.Sum(x => x.Error)
})
.OrderBy(x => x.Date)
.ToList();
}
修改强>
以上将创建一个匿名类型。
如果您想获得List<Models.deployments>
,您应该在选择lambda中执行此操作:
.Select (g => new Models.deployments {
SiteCode = g.Key.SiteCode,
Date = g.Key.date,
SoftwareName = g.Key.softwarename,
Success = g.Sum(x => x.Success),
Error = g.Sum(x => x.Error)
})