使用LINQ处理C#字典
如何使用LINQ Lambda表达式/Statemene表达式编写“//Display using Foreach”循环实现 我希望简化我的开发并尽可能避免嵌套的foreach循环。我试图在第二个foreach语句中包含更多的逻辑,并希望使用Lambda/语句表达式使用LINQ处理C#字典,c#,linq,C#,Linq,如何使用LINQ Lambda表达式/Statemene表达式编写“//Display using Foreach”循环实现 我希望简化我的开发并尽可能避免嵌套的foreach循环。我试图在第二个foreach语句中包含更多的逻辑,并希望使用Lambda/语句表达式 internal class Program { internal class Country { public string CountryName { get; set; } p
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();
}
内部类程序
{
内部阶级国家
{
公共字符串CountryName{get;set;}
公共国际国家代码{get;set;}
}
静态void Main(字符串[]参数)
{
列表国家=新列表()
{
新国家{CountryName=“印度”,CountryCode=1},
新国家{CountryName=“Andaman Nicobar”,国家代码=1},
新国家{CountryName=“美利坚合众国”,CountryCode=2},
新国家{CountryName=“阿拉斯加”,CountryCode=2},
新国家{CountryName=“夏威夷”,CountryCode=2},
新国家{CountryName=“联合王国”,CountryCode=3},
新国家{CountryName=“澳大利亚”,CountryCode=4}
};
字典countriesDictionary=新字典();
foreach(国家中的国家)
{
如果(!countriesDictionary.ContainsKey(country.CountryCode))
countriesDictionary.Add(country.CountryCode,new List());
countriesDictionary[country.CountryCode]。添加(国家);
}
//使用Foreach显示
foreach(countries中的int键dictionary.Keys)
{
List dCountries=countriesDictionary[key];
foreach(数据国家中的国家)
{
if(country.CountryCode.Equals(键))
{
Console.WriteLine(country.CountryName);
}
}
Console.WriteLine();
}
Console.ReadLine();
}
请建议。如果您想按代码对国家进行分组,则不需要两本词典。请使用 或者只需使用您的
countriesDictionary
(它已经有按代码分组的国家):
这是另一种选择:
countriesDictionary.ToList().ForEach
(
pair =>
{
pair.Value.ForEach(country => Console.WriteLine(country.CountryName));
Console.WriteLine();
}
);
此外,这一点基于(答案已删除):
或者让它变得非常简单…正如前面提到的,您已经按照国家代码进行分组
foreach (var country in countriesDictionary.SelectMany(pair => pair.Value))
{
Console.WriteLine(country.CountryName);
}
我将使用一个扩展来做这件事
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);
});
}
}
静态类扩展
{
公共静态无效WriteCountriesGroupyCountryCode(此IEnumerable列表)
{
int currentCountry=int.MinValue;
list.OrderBy(c=>c.CountryCode).ThenBy(c=>c.CountryName.ToList().ForEach(c=>
{
如果(currentCountry==int.MinValue)
{
currentCountry=c.CountryCode;
}
else if(currentCountry!=c.CountryCode)
{
Console.WriteLine();
currentCountry=c.CountryCode;
}
Console.WriteLine(c.CountryName);
});
}
}
一种方法是这样的,避免使用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();
Dictionary countries Dictionary=countries.GroupBy(g=>g.CountryCode.ToDictionary(k=>k.Key,k=>k.ToList());
foreach(countries中的int键dictionary.Keys)
{
Console.WriteLine(“****国家,代码为{0}****”,键);
整数计数=0;
while(count
虽然您目前可能有足够的答案,但此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);
List countryNames=countriesDictionary.SelectMany(
pair=>pair.Value.Where(
country=>country.CountryCode==pair.Key
).Select(x=>x.CountryName)).ToList();
foreach(countryNames中的变量名称)
Console.WriteLine(名称);
但是,按照字典的设置方式,键应该始终与值中的国家代码匹配,对吗?您已经在
countriesDictionary
字典中筛选了国家,为什么要在嵌套循环中搜索dCountries
?
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);
});
}
}
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();
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);