分组到键值对,其中value是共享相同键的元素列表

问题描述

|| 如何从字段列表中创建一个“复杂”对象 例如:
public class Lines {
  public string itemName;
  public string locationName;
  public string locationGeo;

}

List<Lines> lines = new List<Lines>(){
    new Lines() {itemName= \"item1\",locationName=\"someloc1\",locationGeo=\"1.11,1.11\"},new Lines() {itemName= \"item1\",locationName=\"someloc2\",locationGeo=\"1.12,1.12\"},locationName=\"someloc3\",locationGeo=\"1.13,1.13\"},new Lines() {itemName= \"item2\",locationName=\"someloc4\",locationGeo=\"1.14,1.14\"},locationName=\"someloc5\",locationGeo=\"1.15,1.15\"}
}
分组为类似示例对象:
public class exampleLines{
    public string itemName;
    public locations locs;
}
附注:此类根本不存在。     

解决方法

        所以你想按
itemName
分组吗?就像是:
var query = lines.GroupBy(x => x.itemName)
      .Select(g => new { ItemName = g.Key,Locations = g.Select(x => new { Name = x.locationName,Geo = x.locationGeo })
                                      .ToList() })
      .ToList();
这有两种匿名类型-一次用于“容器”类,一次用于“位置”部分。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...