Datatable:根据datetime选择值

本文关键字:选择 datetime 根据 Datatable | 更新日期: 2023-09-27 18:17:54

使用以下值创建一个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) ?

在我的例子中我应该得到Sum(Quantity) = 100 (87 + 13)

Luigi

Datatable:根据datetime选择值

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