选择最大年龄 C#

发布于 2024-08-06 15:15:45 字数 196 浏览 4 评论 0原文

我创建了一个 list,其中包含具有以下属性的对象集合:XYZ >

我想找出集合中哪个对象具有最大的 Z

我尝试使用 Max() 函数,但我不明白它是如何使用的...

I created a list<T> that contains a collection of objects that have this properties: X, Y, Z

I want to find out which object in the collection has the greatest Z

I tried to use the Max() function but I don't understand how it's used...

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

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

发布评论

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

评论(5

征﹌骨岁月お 2024-08-13 15:15:45

Max 用于查找属性的最大值。一旦获得最大值,您就可以使用Where 子句选择值匹配的对象。

var maxZ = list.Max( obj => obj.Z );
var maxObj = list.Where( obj => obj.Z == maxZ );

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.

var maxZ = list.Max( obj => obj.Z );
var maxObj = list.Where( obj => obj.Z == maxZ );
知你几分 2024-08-13 15:15:45

要获取具有最大 Z 值的对象,请按 Z 值降序排序并获取第一项:

TypeOfObject oldest = list.OrderByDescending(x => x.Z).First();

编辑:
将其更改为使用 IEnumerable.OrderByDescending 方法而不是 List.Sort。

编辑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);
   }
}

To get the object with the greatest Z value you sort on the Z value in descending order and get the first item:

TypeOfObject oldest = list.OrderByDescending(x => x.Z).First();

Edit:
Changed it to use the IEnumerable.OrderByDescending method instead of List.Sort.

Edit 2:
If you want performance, this is about four times faster than the fastest LINQ solution:

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);
   }
}
守望孤独 2024-08-13 15:15:45
int maxAge = myList.Max(obj => obj.Z);

使用的参数是 lambda 表达式。该表达式指示使用哪个属性(在本例中为 Z)来获取最大值。这将获得最大年龄。如果您想要具有最大年龄的对象,您可以按年龄排序并获得第一个结果:

MyType maxItem = myList.OrderByDescending(obj => obj.Z).First();

如果您这样做,请注意,如果两个或多个项目都具有最大年龄,则仅选择其中一个。

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:

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.

桜花祭 2024-08-13 15:15:45

如果可以按照以下方式在您的类上实现 IComparableIComparable

public Int32 CompareTo(MyClass other) {
  return Z.CompareTo(other.Z);
}

您只需通过调用即可获取最大值(这将需要 使用 System. Linq):

MyClass maxObject = list.Max();

这只会对您的值列表执行一次传递。

If can implement either IComparable<T> or IComparable on your class along these lines:

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

MyClass maxObject = list.Max();

This will only perform a single pass over your list of values.

美胚控场 2024-08-13 15:15:45

如果具有可比性,这在很大程度上取决于 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);

It will largely depend on the type of X, if it is comparable:

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