C#:Groupby,数据集中数据的差异
C#: Groupby, Difference of data in dataset
以下是我从数据库中获取的数据集:
Id -- Date -- ClockIn -- ClockOut
1 -- 1/1/2016 -- 1/1/2016:09:00:00 -- 1/1/2016:17:03:00
1 -- 1/2/2016 -- 1/1/2016:09:00:00 -- 1/1/2016:11:30:00
1 -- 1/2/2016 -- 1/1/2016:13:00:00 -- 1/1/2016:15:03:00
预期结果
1 -- 1/1/2016(8 hrs) -- 09:00:00 -- 17:03:00
1 -- 1/2/2016(4 hrs) -- 09:00:00 -- 11:30:00
-- 13:00:00 -- 15:03:00
问题: 我想要一个日期字段的分组依据和 clockIn/out 的差值及其当天的总和
这是我正在尝试但无法正常工作的代码:
var resultSet = from newSet in Dtinfo.AsEnumerable()
group newSet by new
{
uid = newSet.Field<int>("UID"),
gDate = newSet.Field<DateTime>("wDate"),
inTime = newSet.Field<DateTime>("punchIn"),
outTime = newSet.Field<DateTime>("punchOut"),
location = newSet.Field<String>("locName"),
typeName = newSet.Field<String>("typeName"),
} into counter
select new
{
UID = counter.Key.uid,
wDate=counter.Key.gDate,
punchIn = counter.Key.inTime,
punchOut = counter.Key.outTime,
locName= counter.Key.location,
typeName= counter.Key.typeName,
diff = (counter.Key.outTime - counter.Key.InTime).TotalHours,
};
我得到了区别,但我也需要 group by 和 sum 才能工作。
路径上的任何光都会有所帮助。
这里是查询。
我用
返回匿名 class
Date
- 按 分组的日期
TotalHours
- 当天所有条目的总和
Entries
- 条目,另一个匿名 class
最好用 classes 替换它们。
var res = from newset in Dtinfo.AsEnumerable()
group newset by newset.Field<DateTime>("wDate")
into counter
select new
{
Date = counter.Key,
TotalHours = counter.Select(a => a.Field<DateTime>("punchOut") - a.Field<DateTime>("punchIn")).Sum(a => a.TotalHours),
Entries = counter.Select(a => new
{
uid = a.Field<int>("UID"),
gDate = a.Field<DateTime>("wDate"),
/*other fields here
*
*
*/
})
};
以下是我从数据库中获取的数据集:
Id -- Date -- ClockIn -- ClockOut
1 -- 1/1/2016 -- 1/1/2016:09:00:00 -- 1/1/2016:17:03:00
1 -- 1/2/2016 -- 1/1/2016:09:00:00 -- 1/1/2016:11:30:00
1 -- 1/2/2016 -- 1/1/2016:13:00:00 -- 1/1/2016:15:03:00
预期结果
1 -- 1/1/2016(8 hrs) -- 09:00:00 -- 17:03:00
1 -- 1/2/2016(4 hrs) -- 09:00:00 -- 11:30:00
-- 13:00:00 -- 15:03:00
问题: 我想要一个日期字段的分组依据和 clockIn/out 的差值及其当天的总和
这是我正在尝试但无法正常工作的代码:
var resultSet = from newSet in Dtinfo.AsEnumerable()
group newSet by new
{
uid = newSet.Field<int>("UID"),
gDate = newSet.Field<DateTime>("wDate"),
inTime = newSet.Field<DateTime>("punchIn"),
outTime = newSet.Field<DateTime>("punchOut"),
location = newSet.Field<String>("locName"),
typeName = newSet.Field<String>("typeName"),
} into counter
select new
{
UID = counter.Key.uid,
wDate=counter.Key.gDate,
punchIn = counter.Key.inTime,
punchOut = counter.Key.outTime,
locName= counter.Key.location,
typeName= counter.Key.typeName,
diff = (counter.Key.outTime - counter.Key.InTime).TotalHours,
};
我得到了区别,但我也需要 group by 和 sum 才能工作。
路径上的任何光都会有所帮助。
这里是查询。 我用
返回匿名 classDate
- 按 分组的日期
TotalHours
- 当天所有条目的总和Entries
- 条目,另一个匿名 class
最好用 classes 替换它们。
var res = from newset in Dtinfo.AsEnumerable()
group newset by newset.Field<DateTime>("wDate")
into counter
select new
{
Date = counter.Key,
TotalHours = counter.Select(a => a.Field<DateTime>("punchOut") - a.Field<DateTime>("punchIn")).Sum(a => a.TotalHours),
Entries = counter.Select(a => new
{
uid = a.Field<int>("UID"),
gDate = a.Field<DateTime>("wDate"),
/*other fields here
*
*
*/
})
};