首页 > 编程语言 > 详细

Linq对列表进行分组,求和,排序

时间:2020-07-04 11:18:37      阅读:50      评论:0      收藏:0      [点我收藏+]

1.分组之后形成一个新的类的列表(分组过程中求和),最后又进行了一次排序

  public List<StaticForestArea> GroupForestList(List<FeatWithBSM> ListBSM)
       {
           var res = ListBSM.GroupBy(x => x.LZ).Select(x =>
                new StaticForestArea
                {           
                    LZ = x.Key,
                    Area = x.Sum(t => (t.geometry as IArea).Area),
                }).ToList();
           res = (from x in res
                  orderby (x.Area) descending
                  select x).ToList();
           return res;
       }   

      public class StaticForestArea
       {
         public  string LZ { get; set; }
         public double Area { get; set; }
       }   

  2.简单的根据面积进行排序

 public  class FeatWithBSM
    {
        public IFeature feature { get; set; }
        public IGeometry geometry { get; set; }
        public string BSM { get; set; }
        public string LZ { get; set; }
        public string ZDGN { get; set; }
        public string TreeCategory { get; set; }
        public double ZXJL { get; set; }
       }
    }
public void OrderList(List<FeatWithBSM> ListBSM)
       {
           var res = (from x in ListBSM
                      orderby (x.geometry as IArea).Area descending
                      select x).ToList<FeatWithBSM>();
                
          this.features= res;
           
       }

  3.对一个大列表根据标识码不同,分成一个个的小列表

 List<FeatWithBSM> featureList = searchFeatures("", layer);
group1 = featureList.GroupBy(x => x.BSM).Select(x =>
                new FeatureCate
                {
                    BSM = x.Key,
                    features = x.ToList()
                }).ToList();
public  class FeatureCate
    {
      
       public List<FeatWithBSM> features;
       public string BSM;
       public double SumArea;

       public string  ZDGN_1="-";//主导功能
       public string ZYSZ_1;//主要树种
       public string LZ_1;//林种
       public double ZXJL_1;//总蓄积量
}

 

  

Linq对列表进行分组,求和,排序

原文:https://www.cnblogs.com/1521681359qqcom/p/13234193.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!