从 List<myType> 获取最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3464934/
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-09-11 01:31:58  来源:igfitidea点击:

Get Max value from List<myType>

listc#-2.0

提问by Waheed

I have List List<MyType>, my type contains Ageand RandomID

我有 List List<MyType>,我的类型包含AgeRandomID

Now I want to find the maximum age from this list.

现在我想从这个列表中找到最大年龄。

What is the simplest and most efficient way?

最简单有效的方法是什么?

采纳答案by Jon Skeet

Okay, so if you don't have LINQ, you could hard-code it:

好的,如果您没有 LINQ,则可以对其进行硬编码:

public int FindMaxAge(List<MyType> list)
{
    if (list.Count == 0)
    {
        throw new InvalidOperationException("Empty list");
    }
    int maxAge = int.MinValue;
    foreach (MyType type in list)
    {
        if (type.Age > maxAge)
        {
            maxAge = type.Age;
        }
    }
    return maxAge;
}

Or you could write a more general version, reusable across lots of list types:

或者您可以编写一个更通用的版本,可在许多列表类型中重用:

public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
{
    if (list.Count == 0)
    {
        throw new InvalidOperationException("Empty list");
    }
    int maxValue = int.MinValue;
    foreach (T item in list)
    {
        int value = projection(item);
        if (value > maxValue)
        {
            maxValue = value;
        }
    }
    return maxValue;
}

You can use this with:

您可以将其用于:

// C# 2
int maxAge = FindMaxValue(list, delegate(MyType x) { return x.Age; });

// C# 3
int maxAge = FindMaxValue(list, x => x.Age);

Or you could use LINQBridge:)

或者你可以使用LINQBridge:)

In each case, you can return the if block with a simple call to Math.Maxif you want. For example:

在每种情况下,您都可以通过简单调用Math.Maxif来返回 if 块。例如:

foreach (T item in list)
{
    maxValue = Math.Max(maxValue, projection(item));
}

回答by Kobi

Assuming you have access to LINQ, and Ageis an int(you may also try var maxAge- it is more likely to compile):

假设您可以访问 LINQ,并且Age是一个int(您也可以尝试var maxAge- 它更有可能编译):

int maxAge = myTypes.Max(t => t.Age);

If you also need the RandomID(or the whole object), a quick solution is to use MaxByfrom MoreLinq

如果您还需要RandomID(或整个对象),一个快速的解决方案是使用MaxByMoreLinq

MyType oldest = myTypes.MaxBy(t => t.Age);

回答by thelost

thelist.Max(e => e.age);

回答by ch007

var maxAge = list.Max(x => x.Age);

回答by Jason Newland

Easiest way is to use System.Linq as previously described

最简单的方法是使用 System.Linq 如前所述

using System.Linq;

public int GetHighestValue(List<MyTypes> list)
{
    return list.Count > 0 ? list.Max(t => t.Age) : 0; //could also return -1
}

This is also possible with a Dictionary

这也可以使用字典

using System.Linq;

public int GetHighestValue(Dictionary<MyTypes, OtherType> obj)
{
    return obj.Count > 0 ? obj.Max(t => t.Key.Age) : 0; //could also return -1
}

回答by Behnam Rasooli

How about this way:

这种方式怎么样:

List<int> myList = new List<int>(){1, 2, 3, 4}; //or any other type
myList.Sort();
int greatestValue = myList[ myList.Count - 1 ];

You basically let the Sort()method to do the job for you instead of writing your own method. Unless you don't want to sort your collection.

您基本上让Sort()方法为您完成工作,而不是编写自己的方法。除非你不想整理你的收藏。

回答by Emile

Simplest is actually just Age.Max(), you don't need any more code.

最简单的其实只是Age.Max(),你不需要更多的代码。