数据表:根据日期时间选择值

时间:2011-12-15 15:17:53

标签: c# select datatable

使用具有这些值的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)

路易

1 个答案:

答案 0 :(得分:3)

使用Linq to DataSet

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"));