c# dictionary return 多个最小最大
c# dictionary return multiple min max
如果字典中的值超过 1 个,如何从 min/max 中获取超过 1 个值?我知道你必须转换 .ToList 才能使用 min/max,但是当我这样做时,它只会给我第一个满足要求的值 min/max.
class Program
{
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
static void Main(string[] args)
{
Program minMaxAge = new Program();
minMaxAge.MinMaxAge();
Console.ReadLine();
}
public Program()
{
myDictionary.Add("Darius", 35);
myDictionary.Add("Caitlin", 25);
myDictionary.Add("Xin", 55);
myDictionary.Add("Alistar", 25);
}
public void MinMaxAge()
{
// Have to convert to list or array in order to get min/max
var ageRange = myDictionary.ToList();
// Created easier to read Keys and Values
var minAge = ageRange.Min(myDictionary => myDictionary.Value);
var minName = myDictionary.FirstOrDefault(x => x.Value == minAge).Key;
var maxAge = ageRange.Max(myDictionary => myDictionary.Value);
var maxName = myDictionary.FirstOrDefault(x => x.Value == maxAge).Key;
Console.WriteLine("The youngest age is {0} and that is {1}.", minAge, minName);
Console.WriteLine("The youngest age is {0} and that is {1}.", maxAge, maxName);
}
}
因为你想获得所有匹配 max
值的项目,你可以使用 Where
而不是 FirstOrDefault
:
var minAge = ageRange.Min(myDictionary => myDictionary.Value);
var minNames = myDictionary.Where(x => x.Value == minAge).Select(p => p.Key);
现在你可以像这样打印所有名字
foreach (string name in minNames) {
Console.WriteLine(name);
}
或者像这样构建一个包含所有名称的 string
:
string allMinNames = string.Join(" ", minNames);
尝试关注dictionary.Where(e => e.Value == maxAge).Select(e => e.key)
您可以通过对用于 MinMaxAge 方法中的名称的 LINQ 指令进行一些小的更改来获得您想要的结果。看看这个:
public void MinMaxAge()
{
// Have to convert to list or array in order to get min/max
var ageRange = myDictionary.ToList();
// Created easier to read Keys and Values
var minAge = ageRange.Min(myDictionary => myDictionary.Value);
var minNames = myDictionary.Where(x => x.Value == minAge)
.Select(x => x.Key)
.Aggregate((current, next) => current + ", " + next);
var maxAge = ageRange.Max(myDictionary => myDictionary.Value);
var maxNames = myDictionary.Where(x => x.Value == maxAge)
.Select(x => x.Key)
.Aggregate((current, next) => current + ", " + next);
Console.WriteLine("The youngest age is {0} and that is {1}.", minAge, minNames);
Console.WriteLine("The youngest age is {0} and that is {1}.", maxAge, maxNames);
}
此致。
您的问题的另一种解决方案如下:
var max = myDictionary.Where(s => s.Value == myDictionary.Max(kvp => kvp.Value));
var min = myDictionary.Where(s => s.Value == myDictionary.Min(kvp => kvp.Value));
这样您就不需要将您的字典转换为列表,也不需要将最大和最小年龄保存在其他已发布解决方案的自己变量中的额外步骤。
如果字典中的值超过 1 个,如何从 min/max 中获取超过 1 个值?我知道你必须转换 .ToList 才能使用 min/max,但是当我这样做时,它只会给我第一个满足要求的值 min/max.
class Program
{
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
static void Main(string[] args)
{
Program minMaxAge = new Program();
minMaxAge.MinMaxAge();
Console.ReadLine();
}
public Program()
{
myDictionary.Add("Darius", 35);
myDictionary.Add("Caitlin", 25);
myDictionary.Add("Xin", 55);
myDictionary.Add("Alistar", 25);
}
public void MinMaxAge()
{
// Have to convert to list or array in order to get min/max
var ageRange = myDictionary.ToList();
// Created easier to read Keys and Values
var minAge = ageRange.Min(myDictionary => myDictionary.Value);
var minName = myDictionary.FirstOrDefault(x => x.Value == minAge).Key;
var maxAge = ageRange.Max(myDictionary => myDictionary.Value);
var maxName = myDictionary.FirstOrDefault(x => x.Value == maxAge).Key;
Console.WriteLine("The youngest age is {0} and that is {1}.", minAge, minName);
Console.WriteLine("The youngest age is {0} and that is {1}.", maxAge, maxName);
}
}
因为你想获得所有匹配 max
值的项目,你可以使用 Where
而不是 FirstOrDefault
:
var minAge = ageRange.Min(myDictionary => myDictionary.Value);
var minNames = myDictionary.Where(x => x.Value == minAge).Select(p => p.Key);
现在你可以像这样打印所有名字
foreach (string name in minNames) {
Console.WriteLine(name);
}
或者像这样构建一个包含所有名称的 string
:
string allMinNames = string.Join(" ", minNames);
尝试关注dictionary.Where(e => e.Value == maxAge).Select(e => e.key)
您可以通过对用于 MinMaxAge 方法中的名称的 LINQ 指令进行一些小的更改来获得您想要的结果。看看这个:
public void MinMaxAge()
{
// Have to convert to list or array in order to get min/max
var ageRange = myDictionary.ToList();
// Created easier to read Keys and Values
var minAge = ageRange.Min(myDictionary => myDictionary.Value);
var minNames = myDictionary.Where(x => x.Value == minAge)
.Select(x => x.Key)
.Aggregate((current, next) => current + ", " + next);
var maxAge = ageRange.Max(myDictionary => myDictionary.Value);
var maxNames = myDictionary.Where(x => x.Value == maxAge)
.Select(x => x.Key)
.Aggregate((current, next) => current + ", " + next);
Console.WriteLine("The youngest age is {0} and that is {1}.", minAge, minNames);
Console.WriteLine("The youngest age is {0} and that is {1}.", maxAge, maxNames);
}
此致。
您的问题的另一种解决方案如下:
var max = myDictionary.Where(s => s.Value == myDictionary.Max(kvp => kvp.Value));
var min = myDictionary.Where(s => s.Value == myDictionary.Min(kvp => kvp.Value));
这样您就不需要将您的字典转换为列表,也不需要将最大和最小年龄保存在其他已发布解决方案的自己变量中的额外步骤。