我有一个表格,其中列出了我需要的小时数。这些条目需要由用户每个月进行。我按不同的用户对条目进行了分组:
var entries = db.ARTCockpit_Global.ToList().Where(u => u.UserDepartmentShort == "TGUCU").GroupBy(x => x.UserName).Distinct();
我需要按月为每个用户编写一个对象数组,例如在Jan,Feb:
之下foreach (var user in entries)
{
object[] value = new object[] { 5, 2 };
allSeries.Add(new Series { Data = new Data(value), Name = user.First().UserName });
}
foreach循环的问题在于它将为每个值创建一个数组,例如: [5],[2]
数据库示例摘录:
Date YYYYMM Year MM DD DDnum UserID UserName Hours
2015-01-12 201501 2015 01 12 12 Milesdc Miles, Dean 5
2015-01-12 201501 2015 01 12 12 MMaslan Maslan, Martin 4
2015-02-05 201502 2015 02 05 5 Milesdc Miles, Dean 2
2015-02-05 201502 2015 02 05 5 MMaslan Maslan, Martin 8
答案 0 :(得分:1)
我假设您的表对应ARTCockpit_Global
对象,尽管它没有UserDepartmentShort
属性。您可以使用linQ对结果进行分组,并将完整的总和分类为:
var total = (from x in db
group x by new { x.Date.Year, x.Date.Month, x.UserId } into grp
select new
{
grp.Key.UserId,
grp.Key.Month,
grp.Key.Year,
Total = grp.Sum(x => x.Hours)
}).ToList();