Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表中的列表聚合为一个列表_C# - Fatal编程技术网

C# 将列表中的列表聚合为一个列表

C# 将列表中的列表聚合为一个列表,c#,C#,我有这些定义 ClassCountryCollection定义为: public class ClassCountryCollection : KeyedCollection<string, ClassCountry> { protected override IEnumerable<string> GetKey(ClassCountry i) { return i.GetKey(i.Key);

我有这些定义

ClassCountryCollection
定义为:

public class ClassCountryCollection : KeyedCollection<string, ClassCountry>
    {
        protected override IEnumerable<string> GetKey(ClassCountry i)
        {
            return i.GetKey(i.Key);
        }
    }
public class ClassCityCollection : KeyedCollection<string, ClassCity>
{
    protected override string GetKeyForItem(ClassCity i)
    {
        return i.Name;
    }
}
ClassCityCollection
定义为:

public class ClassCountryCollection : KeyedCollection<string, ClassCountry>
    {
        protected override IEnumerable<string> GetKey(ClassCountry i)
        {
            return i.GetKey(i.Key);
        }
    }
public class ClassCityCollection : KeyedCollection<string, ClassCity>
{
    protected override string GetKeyForItem(ClassCity i)
    {
        return i.Name;
    }
}
我有一个类型为
ClassCountryCollection
country

1. Key=Spain, ClassCityCollection{{Population=55,Name=Barcelona},{Population=65,Name=Madrid}}
2. Key=Germany, ClassCityCollection{{Population=12,Name=Berlin},{Population=125,Name=Dresden}}
我想创建一个列表,使上面的内容

1. {"Spain|Population=55,Name=Barcelona,Population=65,Name=Madrid"}
2. {"Germany|Population=12,Name=Berlin,Population=125,Name=Dresden"}
我试过了

country.Select(x => new { x.ClassCityList.Select(w => string.Join(",",w.ToString().ToList())), x.Key }).ToList();

然后我陷入困境,不知道如何继续。也许我在那里的某个地方创建了
ToString()
,使它更简单。

我假设了类结构并返回了这段代码

    namespace welcome
   {

class Program
{
    static void Main(string[] args)
    {

        ClassCountryCollection cc = new ClassCountryCollection();

        var c1 = new ClassCountry()
        {
            Key = "Spain",
            ClassCityList = new ClassCityCollection{
                new ClassCity{Name ="Barcelona", Population = 55},
                new ClassCity{Name ="Madrid", Population = 65},
            }
        };

        var c2 = new ClassCountry()
        {
            Key = "Germany",
            ClassCityList = new ClassCityCollection{
                new ClassCity{Name ="Berlin", Population = 12},
                new ClassCity{Name ="Dresden", Population = 125},
            }
        };

        cc.Add(c1);
        cc.Add(c2);

        var g = cc.Select(x => x.Key + "|" + x.ClassCityList.Select(x => nameof(x.Population) + "=" + x.Population + "," + nameof(x.Name) + "=" + x.Name).Aggregate((d, s) => d + "," + s));
        foreach (var item in g)
        {
            System.Console.WriteLine(item);
        }
        Console.ReadLine();
    }
}

public class ClassCountryCollection : KeyedCollection<string, ClassCountry>
{

    protected override string GetKeyForItem(ClassCountry item)
    {
        return item.Key;
    }

}
public class ClassCountry
{
    public string Key { get; set; }
    public ClassCityCollection ClassCityList { get; set; }
}

public class ClassCityCollection : KeyedCollection<string, ClassCity>
{
    protected override string GetKeyForItem(ClassCity i)
    {
        return i.Name;
    }
}


public class ClassCity
{
    public int Population { get; set; }
    public string Name { get; set; }

}
}
按键然后按名称排序时 输出为

  Germany|Population=12,Name=Berlin,Population=125,Name=Dresden
  Spain|Population=55,Name=Barcelona,Population=65,Name=Madrid
代码需要修改为

  var g = cc.OrderBy(x=>x.Key).Select(x=>x.Key + "|" + x.ClassCityList.OrderBy(x=>x.Name).Select(x=>nameof(x.Population) +"=" + x.Population+","+nameof(x.Name) +"="+x.Name).Aggregate((d,s)=>d+","+s)); 

我假设类结构并返回此代码

    namespace welcome
   {

class Program
{
    static void Main(string[] args)
    {

        ClassCountryCollection cc = new ClassCountryCollection();

        var c1 = new ClassCountry()
        {
            Key = "Spain",
            ClassCityList = new ClassCityCollection{
                new ClassCity{Name ="Barcelona", Population = 55},
                new ClassCity{Name ="Madrid", Population = 65},
            }
        };

        var c2 = new ClassCountry()
        {
            Key = "Germany",
            ClassCityList = new ClassCityCollection{
                new ClassCity{Name ="Berlin", Population = 12},
                new ClassCity{Name ="Dresden", Population = 125},
            }
        };

        cc.Add(c1);
        cc.Add(c2);

        var g = cc.Select(x => x.Key + "|" + x.ClassCityList.Select(x => nameof(x.Population) + "=" + x.Population + "," + nameof(x.Name) + "=" + x.Name).Aggregate((d, s) => d + "," + s));
        foreach (var item in g)
        {
            System.Console.WriteLine(item);
        }
        Console.ReadLine();
    }
}

public class ClassCountryCollection : KeyedCollection<string, ClassCountry>
{

    protected override string GetKeyForItem(ClassCountry item)
    {
        return item.Key;
    }

}
public class ClassCountry
{
    public string Key { get; set; }
    public ClassCityCollection ClassCityList { get; set; }
}

public class ClassCityCollection : KeyedCollection<string, ClassCity>
{
    protected override string GetKeyForItem(ClassCity i)
    {
        return i.Name;
    }
}


public class ClassCity
{
    public int Population { get; set; }
    public string Name { get; set; }

}
}
按键然后按名称排序时 输出为

  Germany|Population=12,Name=Berlin,Population=125,Name=Dresden
  Spain|Population=55,Name=Barcelona,Population=65,Name=Madrid
代码需要修改为

  var g = cc.OrderBy(x=>x.Key).Select(x=>x.Key + "|" + x.ClassCityList.OrderBy(x=>x.Name).Select(x=>nameof(x.Population) +"=" + x.Population+","+nameof(x.Name) +"="+x.Name).Aggregate((d,s)=>d+","+s)); 

您的示例无法编译。class
KeyCollection
是否来自standart图书馆?我猜你的意思是
KeyedCollection
。您是否希望生成的集合的类型为
IEnumerable
?请提供最小的可复制示例。是的,这就是
KeyedCollection
。不需要数不清。我不能复制这个,因为我是从一个大项目里拿的。定义所有可复制的内容需要十几个页面,您是否希望结果是元素的集合,其中每个元素都有两个字段:第一个字段-
字符串CountryName
,第二个字段-
字符串Cities
,用逗号分隔?您的示例无法编译。class
KeyCollection
是否来自standart图书馆?我猜你的意思是
KeyedCollection
。您是否希望生成的集合的类型为
IEnumerable
?请提供最小的可复制示例。是的,这就是
KeyedCollection
。不需要数不清。我不能复制这个,因为我是从一个大项目里拿的。定义所有可复制的内容需要十几页,您是否希望结果是元素的集合,其中每个元素有两个字段:第一个字段-
字符串CountryName
,第二个字段-
字符串Cities
,用逗号分隔?谢谢。我做了一些小改动,它完全符合我的需要。这个问题有点离题,所以这对我来说太完美了<代码>抄送选择(x=>x.Key+“|”+x.ClassCityList.Select(k=>k.Name+“=”+k.Population)。聚合((d,s)=>d+,“+s)).ToList()。若你们不介意的话,有一件事,我怎样才能确保上面的列表是按x键排序的,然后是按k名排序的?谢谢。我做了一些小改动,它完全符合我的需要。这个问题有点离题,所以这对我来说太完美了<代码>抄送选择(x=>x.Key+“|”+x.ClassCityList.Select(k=>k.Name+“=”+k.Population)。聚合((d,s)=>d+,“+s)).ToList()。若你们不介意的话,有一件事,我怎样才能确保上面的列表是按x.Key排序的,然后是按k.Name排序的?