使用具有这些值的DataTable(例如):
Quantity ReferenceDate
87.000000 2011-11-24 15:12:26.990
58.000000 2011-11-24 15:12:26.030
87.000000 2011-11-24 12:18:27.180
58.000000 2011-11-24 13:12:27.027
87.000000 2011-11-24 19:22:27.563
13.000000 2011-11-24 19:40:27.123
拥有只有小时的DateTime
变量,例如
DateTime mySelectDate = new DateTime (2011,12,15,19,00,00)
我怎样才能获得sum(Quantity)
以获得" fall"在那个小时?
在我的例子中,我应该得到Sum(Quantity) = 100 (87 + 13)
路易
答案 0 :(得分:3)
DateTime mySelectDate = new DateTime (2011,12,15,19,00,00);
DateTime endDate = mySelectDate.AddHours(1);
var sum = table.Rows.AsEnumerable().
.Where(row =>
{
var referenceDate = row.Field<DateTime>("ReferenceDate");
return ((referenceDate >= mySelectDate ) && (referenceDate < endDate));
})
.Sum(row => row.Field<double>("Quantity"));