Linq 中的获取最大值得记录

it2026-08-14  5

这是我写的第一个Linq 语句。写的比较慢。因为经常写sql现在写Linq 总是感觉不顺手。

不说了,下面就是我的一个Linq语句。

获得每组的记录数:

  var b  =  from a  in  List1                    let pcount  =  List2.Where(s  =>  s.CenterId  ==  a.CenterId).Count()                     where  a.CenterId != 0                     select  new                     {                      groupID = 1 ,                      CenterID = a.CenterId,                      lal  =  a.Latitude,                      lo  =  a.Longitude,                      isHave  =  a.HasData,                      countC  =  pcount                    };

 获取组中记录最大的

 var c  =  from d  in  b                    group d by d.groupID into f                    let e  =  f.Max(d  =>  d.countC)                    from row  in  f                     where  row.countC  ==  e                    select row;

 

读取数据

 

 if (c.Count() > 0)             {                 double a = c.ToList()[0].lal;               }

转载于:https://www.cnblogs.com/lfzwenzhu/archive/2011/05/30/2063188.html

相关资源:按某一字段分组取最大(小)值所在行的数据
最新回复(0)