从 List获取最大值

发布于 2024-09-14 04:10:10 字数 142 浏览 7 评论 0原文

我有列表 List,我的类型包含 AgeRandomID

现在我想从此列表中查找最大年龄。

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

I have List List<MyType>, my type contains Age and RandomID

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

What is the simplest and most efficient way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

深爱成瘾 2024-09-21 04:10:10

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

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

如果您还需要 RandomID (或整个对象),快速解决方案是使用 MaxBy 来自 更多Linq

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

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

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

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

MyType oldest = myTypes.MaxBy(t => t.Age);
淡淡绿茶香 2024-09-21 04:10:10

好的,所以如果您没有 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;
}

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

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;
}

您可以将其用于:

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

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

或者您可以使用 LINQBridge :)

在每种情况下,您都可以通过简单调用 Math.Max 如果你愿意的话。例如:

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

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

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 :)

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

foreach (T item in list)
{
    maxValue = Math.Max(maxValue, projection(item));
}
胡渣熟男 2024-09-21 04:10:10
var maxAge = list.Max(x => x.Age);
var maxAge = list.Max(x => x.Age);
狠疯拽 2024-09-21 04:10:10
thelist.Max(e => e.age);
thelist.Max(e => e.age);
深海不蓝 2024-09-21 04:10:10

最简单的方法是使用前面描述的 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
}

这也可以使用字典

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
}

Easiest way is to use System.Linq as previously described

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
}
留蓝 2024-09-21 04:10:10

最简单的实际上只是 Age.Max(),您不需要任何更多代码。

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

圈圈圆圆圈圈 2024-09-21 04:10:10

这样怎么样:

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

基本上让 Sort() 方法为您完成这项工作,而不是编写自己的方法。除非你不想对你的收藏进行排序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文