C# 如何从字典中获取 MAX 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10290838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:12:40  来源:igfitidea点击:

How to get MAX value from Dictionary?

c#.netlinqdictionary

提问by Terminador

I have

我有

Dictionary<Guid, DateTime> d = new Dictionary<Guid, DateTime>();

How I can get an Guidwhich has MAXvalue?

我怎样才能得到一个Guid具有MAX价值?

采纳答案by Asik

Since this was the accepted answer, I'll try to cover every possible meaning of the question:

由于这是公认的答案,我将尝试涵盖问题的所有可能含义:

var dict = new Dictionary<string, int> { { "b", 3 }, { "a", 4 } };

// greatest key
var maxKey = dict.Keys.Max(); // "b"

// greatest value
var maxValue = dict.Values.Max(); // 4

// key of the greatest value
// 4 is the greatest value, and its key is "a", so "a" is the answer.
var keyOfMaxValue = dict.Aggregate((x, y) => x.Value > y.Value ? x : y).Key; // "a"

Note: the question has System.Guidas the key type. It might not make sense to ask "what is the greatest GUID", since they are simply intended to be unique values, rather than represent any orderable concept. Nonetheless, the above code will work with any type that supports the >operator, stringand intbeing chosen here for conciseness.

注意:问题有System.Guid作为关键类型。问“最大的 GUID 是什么”可能没有意义,因为它们只是旨在成为唯一值,而不是代表任何可排序的概念。尽管如此,上面的代码将与支持任何类型的工作>操作者,string并且int在这里被选择为简明。

回答by Alexei Levenkov

Guid implements IComparable, so:

Guid 实现了 IComparable,所以:

d.Keys.Max()

Also it is not clear why one would want to do so...

也不清楚为什么要这样做......

回答by Steven P

            var maxGuid = Guid.Empty;
            var maxDateTime = DateTime.MinValue;
            foreach (var kvp in d)
            {
                if (kvp.Value > maxDateTime)
                {
                    maxGuid = kvp.Key;
                    maxDateTime = kvp.Value;
                }
            }
            Console.WriteLine("Guid of max date is: " + maxGuid.ToString());

回答by Papa Burgundy

This works great. It will return the GUID for the MAX date.

这很好用。它将返回 MAX 日期的 GUID。

Dictionary<Guid, DateTime> d = new Dictionary<Guid, DateTime>(); 
var guidForMaxDate = d.FirstOrDefault(x => x.Value == d.Values.Max()).Key;

回答by Sai

Another way might helps to get Single KeyValuePair.

另一种方法可能有助于获得 Single KeyValuePair。

KeyValuePair<char, int> GuidKeyPair = guidDict.FirstOrDefault( MaxGuid => MaxGuid.Value == guidDict.Values.Max());

回答by felixperreault

Ordering your data in the first place could be one solution.

首先对数据进行排序可能是一种解决方案。

var maxGuid = d.OrderByDescending(x => x.Value).FirstOrDefault().Key;

回答by GDB

The accepted answer didn't work for me. The following code (using MoreLinq) did the job though:

接受的答案对我不起作用。不过,以下代码(使用 MoreLinq)完成了这项工作:

var fooDict = new Dictionary<string, int>();
var keyForBiggest = fooDict.MaxBy(kvp => kvp.Value).Key;
var biggestInt = fooDict[keyForBiggest];

回答by Hamza Khanzada

By using LINQ on dictionary.

通过在字典上使用 LINQ。

var MaximumValue = dict.FirstOrDefault(x => x.Value.Equals(dict.Values.Max()));