使用LINQ处理C#字典

Sri*_*m B 19 c# linq

如何使用LINQ Lambda Expression/Statemene Expression编写"//使用Foreach显示"循环实现?

我想简化我的开发并尽可能避免嵌套的foreach循环.我试图在第二个foreach语句中包含更多逻辑,我想使用Lambda/Statement表达式.

 internal class Program
{
    internal class Country
    {
        public string CountryName { get; set; }
        public int CountryCode { get; set; }
    }
    static void Main(string[] args)
    {
        List<Country> countries = new List<Country>()
        {
            new Country{CountryName = "India", CountryCode=1},
            new Country{CountryName = "Andaman Nicobar", CountryCode=1},
            new Country{CountryName = "United States of America",  CountryCode=2},
            new Country{CountryName = "Alaska",  CountryCode=2},
            new Country{CountryName = "Hawaii",  CountryCode=2},
            new Country{CountryName = "United Kingdom", CountryCode=3},
            new Country{CountryName = "Australia", CountryCode=4}
        };

        Dictionary<int, List<Country>> countriesDictionary = new Dictionary<int, List<Country>>();
        foreach (Country country in countries)
        {
            if (!countriesDictionary.ContainsKey(country.CountryCode))
                countriesDictionary.Add(country.CountryCode, new List<Country>());
            countriesDictionary[country.CountryCode].Add(country);                
        }

       // Display using Foreach

        foreach (int key in countriesDictionary.Keys)
        {                             
            List<Country> dCountries = countriesDictionary[key];

            foreach (Country country in dCountries)
            {
                if (country.CountryCode.Equals(key))
                {
                    Console.WriteLine(country.CountryName);
                }                
            }
            Console.WriteLine();            
        }
        Console.ReadLine();
    }
Run Code Online (Sandbox Code Playgroud)

请建议.

The*_*aot 17

这是另一种选择:

countriesDictionary.ToList().ForEach
(
    pair =>
    {
        pair.Value.ForEach(country => Console.WriteLine(country.CountryName));
        Console.WriteLine();
    }
);
Run Code Online (Sandbox Code Playgroud)

此外,这个基于Romoku的答案(答案被删除):

var countriesDictionary = countries.ToLookup(x => x.CountryCode, x => x);

foreach(var countryGroup in countriesDictionary)
{
    foreach(var country in countryGroup)
    {
        Console.WriteLine(country.CountryName);
    }
    Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)


Ser*_*kiy 6

如果您想按代码对国家/地区进行分组,那么您不需要两个词典.使用Enumerable.GroupBy

foreach(var codeGroup in countries.GroupBy(c => c.CountryCode))
{
    foreach(var country in codeGroup)
       Console.WriteLine(country.CountryName);

    Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)

或者只使用您的countriesDictionary(已经有按代码分组的国家/地区):

foreach(var kvp in countriesDictionary)
{
    foreach(var country in kvp.Value)
       Console.WriteLine(country.CountryName);

    Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)