Linq对列表进行分组,求和,排序
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;//总蓄积量 }