日期:2014-05-20  浏览次数:20880 次

DataTable查询
value | time
 25.2 | 2011-05-15 19:50:12
 23.6 | 2011-05-15 19:50:58
 25.8 | 2011-05-15 19:51:23
 24.5 | 2011-05-15 19:51:51

关于这样结构的表,如何用linq来查
最大值:
value | time
25.8 | 2011-05-15 19:51:23
最小值:
value | time
23.6 | 2011-05-15 19:50:58
平均值
value
24.775

------解决方案--------------------
http://topic.csdn.net/u/20090329/20/10391661-1721-41c2-a9e7-f183c38254e7.html
我觉的可以参考这个
------解决方案--------------------
var max = (from x in entities orderby x.value select x).Take(1);
var min = (from x in entities descending orderby x.value descting select x).Take(1);
var avg = new { value = entities.Average(x => x.value) };
------解决方案--------------------
C# code

void Main()
{
  DataTable dt=new DataTable();
  dt.Columns.Add("value",typeof(double));
  dt.Columns.Add("time",typeof(string));
  dt.Rows.Add(25.2,"2011-05-15 19:50:12");
  dt.Rows.Add(23.6,"2011-05-15 19:50:58");
  dt.Rows.Add(25.8,"2011-05-15 19:51:23");
  dt.Rows.Add(24.5,"2011-05-15 19:51:51");
  
  var max=dt.AsEnumerable().OrderByDescending(r=>r.Field<double>("value")).FirstOrDefault();
  
  var min=dt.AsEnumerable().OrderBy(r=>r.Field<double>("value")).FirstOrDefault();
  
  var average=dt.AsEnumerable().Average(r=>r.Field<double>("value"));
  
   
}