C# Collection 选择属性的值与另一个属性的最小值

2024-01-12

所以假设我有一种类型Car有两个属性Speed and Color

public class Car
{
   public int Speed {get; set;}
   public string Color {get; set;}
}

使用 LINQ 我可能会找到最低速度

int minSpeed = collection.Min(em => em.Speed);

所以 minSpeed 将包含集合中速度最小的汽车的速度值。

但我怎样才能做类似的事情来获得汽车的颜色呢?

就像是:

string color = collection.Min(em => em.Speed).Select(x => x.Color);

Use MinBy https://code.google.com/p/morelinq/source/browse/MoreLinq/MinBy.cs.

Car slowestCar = collection.MinBy(em => em.Speed);
string color = slowestCar.Color;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# Collection 选择属性的值与另一个属性的最小值 的相关文章

随机推荐