2013-07-05 65 views
10

如何使用LINQ Lambda Expression/Statemene Expression編寫「//使用Foreach顯示」循環實現?使用LINQ處理C#字典

我想簡化我的開發並儘可能避免嵌套的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(); 
    } 

請建議。

+5

您已在'countriesDictionary'字典中過濾國家,爲什麼在嵌套循環中搜索'dCountries'? –

回答

5

如果你想通過代碼對國家進行分組,那麼你不需要兩個字典。使用Enumerable.GroupBy

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

    Console.WriteLine(); 
} 

或者只是使用您的countriesDictionary(它已經國家的代碼分組):

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

    Console.WriteLine(); 
} 
10

這是另一種選擇:

countriesDictionary.ToList().ForEach 
(
    pair => 
    { 
     pair.Value.ForEach(country => Console.WriteLine(country.CountryName)); 
     Console.WriteLine(); 
    } 
); 

此外,這一個基於 Romoku's Answer (a nswer移除):

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

var dCountries = new List<Country>(); 

foreach(var countryGroup in countriesDictionary) 
{ 
    foreach(var country in countryGroup) 
    { 
     Console.WriteLine(country.CountryName); 
    } 
    Console.WriteLine(); 
} 
1

或者只是使它很容易...前面已經提到,你已經通過國家代碼分組...

 foreach (var country in countriesDictionary.SelectMany(pair => pair.Value)) 
     { 
      Console.WriteLine(country.CountryName); 
     } 
0

我會用一個擴展做它。

static class CountryExtension 
    { 
     public static void WriteCountriesGroupyCountryCode(this IEnumerable<Country> list) 
     { 
      int currentCountry=int.MinValue; 
      list.OrderBy(c => c.CountryCode).ThenBy(c=>c.CountryName).ToList().ForEach(c => 
      { 
       if (currentCountry == int.MinValue) 
       { 
        currentCountry = c.CountryCode; 
       } 
       else if (currentCountry != c.CountryCode) 
       { 
        Console.WriteLine(); 
        currentCountry = c.CountryCode; 
       } 
       Console.WriteLine(c.CountryName); 
      }); 
     } 
    } 
1

的一種方法是這樣的,避免與的GroupBy第一的foreach,剛剛1的foreach邏輯打印每個國家名稱與指定的代碼:

Dictionary<int, List<Country>> countriesDictionary = countries.GroupBy(g => g.CountryCode).ToDictionary(k => k.Key, k => k.ToList()); 

foreach (int key in countriesDictionary.Keys) 
{ 
     Console.WriteLine("****Countries with code {0}****",key); 
     int count = 0; 
     while (count < countriesDictionary[key].Count) 
     { 
      Console.WriteLine(countriesDictionary[key][count].CountryName); 
      count++; 
     } 
     Console.WriteLine(); 
} 

Console.ReadLine(); 
1

雖然你可能有足夠的答案現在,這個LINQ會將你的字典壓縮到一個國家名稱列表中,允許一個簡單的foreach顯示它們。

List<string> countryNames = countriesDictionary.SelectMany(
     pair=>pair.Value.Where(
      country=>country.CountryCode == pair.Key 
     ).Select(x=>x.CountryName)).ToList(); 

    foreach (var name in countryNames) 
     Console.WriteLine(name); 

但是你的字典設置的方式,關鍵要始終匹配值的國家代碼,是否正確?