c#字典返回多个最小值

时间:2016-01-09 18:23:00

标签: c# list dictionary max min

如果超过1,如何从字典中获得超过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);
    }
}

4 个答案:

答案 0 :(得分:3)

由于您希望获取与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) 

答案 1 :(得分:0)

尝试关注class Operations trait ExpAlg { type Opr <: Operations def lit(x: Int): Opr } trait Eval extends Operations { def eval(): Int } trait EvalExpAlg extends ExpAlg { type Opr = Eval def lit(x: Int) = new Eval() { def eval() = x } }

答案 2 :(得分:0)

您可以通过对用于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);
}

问候。

答案 3 :(得分:0)

您的问题的另一个解决方案如下:

var max = myDictionary.Where(s => s.Value == myDictionary.Max(kvp => kvp.Value));
var min = myDictionary.Where(s => s.Value == myDictionary.Min(kvp => kvp.Value));

这样您就不需要将Dictionary转换为List,也无需从其他已发布的解决方案中删除自己变量中保存最大和最小年龄的额外步骤。