选择最大年龄 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1489126/
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
Select max age C#
提问by Killercode
I created a list<T>
that contains a collection of objects that have this properties: X
, Y
, Z
我创建了一个list<T>
包含有这个属性的对象的集合:X
,Y
,Z
I want to find out which object in the collection has the greatest Z
我想找出集合中哪个对象最大 Z
I tried to use the Max()
function but I don't understand how it's used...
我尝试使用该Max()
功能,但我不明白它是如何使用的...
采纳答案by tvanfosson
Max is used to find the maximum value of a property. Once you have the maximum value you can select those objects whose value matches using the Where clause.
Max 用于查找属性的最大值。获得最大值后,您可以使用 Where 子句选择那些值匹配的对象。
var maxZ = list.Max( obj => obj.Z );
var maxObj = list.Where( obj => obj.Z == maxZ );
回答by Meta-Knight
int maxAge = myList.Max(obj => obj.Z);
The parameter used is a lambda expression. This expression indicates which property to use(in this case, Z) to get the max value. This will get the maximum age. If you want the object which has the greatest age, you could sort by age and get the first result:
使用的参数是一个 lambda 表达式。此表达式指示使用哪个属性(在本例中为 Z)来获取最大值。这将获得最大年龄。如果你想要年龄最大的对象,你可以按年龄排序并得到第一个结果:
MyType maxItem = myList.OrderByDescending(obj => obj.Z).First();
If you do it this way, note that if two or more items all have the maximum age, only one of them is selected.
如果这样做,请注意,如果两个或多个项目都具有最大年龄,则只会选择其中一个。
回答by Darin Dimitrov
It will largely depend on the type of X, if it is comparable:
如果它具有可比性,它将在很大程度上取决于 X 的类型:
var array = new[] {
new { X = "a", Y = "a", Z = 1 },
new { X = "b", Y = "b", Z = 2 }
};
var max = array.Max(x => x.Z);
Console.WriteLine(max);
回答by Guffa
To get the object with the greatest Z value you sort on the Z value in descending order and get the first item:
要获得具有最大 Z 值的对象,您可以按降序对 Z 值进行排序并获得第一项:
TypeOfObject oldest = list.OrderByDescending(x => x.Z).First();
Edit:
Changed it to use the IEnumerable.OrderByDescending method instead of List.Sort.
编辑:
将其更改为使用 IEnumerable.OrderByDescending 方法而不是 List.Sort。
Edit 2:
If you want performance, this is about four times faster than the fastest LINQ solution:
编辑 2:
如果你想要性能,这比最快的 LINQ 解决方案快四倍:
int high = Int32.MinValue;
List<TypeOfObject> highest = new List<TypeOfObject>();
foreach (TypeOfObject v in list) {
if (v.Z >= high) {
if (v.Z > high) highest.Clear();
high = v.Z;
highest.Add(v);
}
}
回答by Martin Liversage
If can implement either IComparable<T>
or IComparable
on your class along these lines:
如果可以按照以下方式在您的课程中实现IComparable<T>
或IComparable
在您的课程上实现:
public Int32 CompareTo(MyClass other) {
return Z.CompareTo(other.Z);
}
You can get the maximum value simply by calling (this will require using System.Linq
):
您只需调用即可获得最大值(这将需要using System.Linq
):
MyClass maxObject = list.Max();
This will only perform a single pass over your list of values.
这只会对您的值列表执行一次传递。