Linq對列表進行分組,求和,排序
阿新 • • 發佈:2020-07-04
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;//總蓄積量 }