List对象中的组合、查询、扩展

var Pnts = segs.Select( c =>
                                             pntTsLst.Where                                                            //根据条件组合另一个List, pntTsLst是一个List<KEYPOINT>
                                             (d => d.KEYPOINT_UUID == c.END_POINT_ID)
                                                                                                                          .Select(
                                                                                                                                           k =>new { id=k.KEYPOINT_UUID,name=k.NAME + (k.DESIGNATOR.IsEmptyOrNull() ? "" : $"({k.DESIGNATOR})"),seq=c.SEQNUM+1 }  //从另一个List中取值并添加其他属性
                                                                                                                                      ).First()      //First非常重要,作用于where,把整个最外层括号中的数据类型变为属性,否则是个两重List结构                            
                                             ).ToList();

//Pnts 是一个自定义对象{id,name,seq} 的list

 

//简单一点List转换的例子 Geoms是一个Geom(id,name,geo)的对象list

 Geoms = dic.Select(c => new Geom{ id=c.Key,geo=c.Value} ).ToList();

 

或者,返回一个扩展的对象

var lst = Pnts.Select(c => new {
                                                      keypt =pntTsLst.Where(d => d.KEYPOINT_UUID == c.id).First(),
                                                      seq =c.seq
                                                 }

                                 ).ToList();

lst 是一个扩展对象{KEYPOINT,seq}的list

posted on 2018-01-12 14:15  mol1995  阅读(210)  评论(0编辑  收藏  举报

导航