在 C# 中查找集合(的属性)的最小值
给出来自 Microsoft 示例的以下代码:
public class EngineMeasurementCollection : Collection<EngineMeasurement>
{
public EngineMeasurementCollection()
{
Add(new EngineMeasurement { Speed = 1000, Torque = 100, Power = 20 });
Add(new EngineMeasurement { Speed = 2000, Torque = 160, Power = 60 });
Add(new EngineMeasurement { Speed = 3000, Torque = 210, Power = 125 });
Add(new EngineMeasurement { Speed = 4000, Torque = 220, Power = 160 });
Add(new EngineMeasurement { Speed = 5000, Torque = 215, Power = 205 });
Add(new EngineMeasurement { Speed = 6000, Torque = 200, Power = 225 });
Add(new EngineMeasurement { Speed = 7000, Torque = 170, Power = 200 });
}
}
public class EngineMeasurement
{
public int Speed { get; set; }
public int Torque { get; set; }
public int Power { get; set; }
}
How do I get the min/maximum of Speed or Torque or Power.我需要它来设置我正在做的图表上的比例(准确地说是 WPF Toolkit Chart)。 我想我可以在 EngineMeasurementCollection 中有一个方法来迭代每个 EngineMeasurement 并查看功率(或速度),但我怀疑还有更简单的方法吗? Collection 类确实有某种 Min 方法,但请注意,我并不是试图获取集合的最小值(我不确定在这种情况下这意味着什么),而是获取特定属性的最小值(例如速度)。我确实看到了 Collection.Min 与函子的使用。那里有什么可以做的吗?或者使用 Linq?我对所有方面都感兴趣。 谢谢, 戴夫
奖金问题(也许这对我来说对于最小/最大的答案来说是显而易见的)。有哪些选项可以决定某个值(例如速度)是否已在集合中。从这个例子中还不清楚,但可能的情况是,如果您已经拥有给定自变量(例如时间)的一些数据,则您不再需要更多数据。那么是否有类似 Collection.Contains("指定您感兴趣的属性") 之类的东西?
Given the following code from a Microsoft example:
public class EngineMeasurementCollection : Collection<EngineMeasurement>
{
public EngineMeasurementCollection()
{
Add(new EngineMeasurement { Speed = 1000, Torque = 100, Power = 20 });
Add(new EngineMeasurement { Speed = 2000, Torque = 160, Power = 60 });
Add(new EngineMeasurement { Speed = 3000, Torque = 210, Power = 125 });
Add(new EngineMeasurement { Speed = 4000, Torque = 220, Power = 160 });
Add(new EngineMeasurement { Speed = 5000, Torque = 215, Power = 205 });
Add(new EngineMeasurement { Speed = 6000, Torque = 200, Power = 225 });
Add(new EngineMeasurement { Speed = 7000, Torque = 170, Power = 200 });
}
}
public class EngineMeasurement
{
public int Speed { get; set; }
public int Torque { get; set; }
public int Power { get; set; }
}
How do I get the minimum/maximum of Speed or Torque or Power. I need this to set the scale on a chart I'm doing (WPF Toolkit Chart to be precise).
I suppose I could have a method inside EngineMeasurementCollection that iterates through each EngineMeasurement and looks at Power (or Speed), but I suspect there is a much easier way? The class Collection does have some sort of Min method, but note, I'm not trying to get the minimum of the collection (I'm not sure what that would mean in this case) but rather, the minimum of a particular property (ex. Speed). I did see the use of Collection.Min with functors. Is there something that could be done there? Or with Linq? I'm interested in all ways.
Thanks,
Dave
Bonus question (perhaps this will be obvious to me with the answer to min/max). What are the options to decide if a value (such as Speed is already in the collection). It's not clear from this example, but it could be the case that if you already have some data for a given independent variable (time for example), you don't want any more. So is there something like Collection.Contains("specify property you are interested in")?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另请参阅:
LINQ MSDN 文档
LINQ to Objects 5 分钟概述
See also:
LINQ MSDN documentation
LINQ to Objects 5 Minute Overview
添加到 gaearon 的答案:
将为您提供最低限度。但您可能会自己弄清楚;)
您可以查看 MSDN 网站上的此链接 详细介绍了使用 linq 查找最大值/最小值。
To add to gaearon's answer:
Will get you the minimum. But you probably would have figured that out on your own ;)
You can take a look at this link on MSDN's site which goes over finding max/min values using linq.
要回答有关“Contains”类型方法的问题,如果您需要布尔值指示其是否存在,则可以使用
Any
方法,或者可以使用FirstOrDefault
来查找第一个满足条件的EngineMeasurement
出现。如果存在,它将返回实际对象,否则将返回该对象的默认值(在本例中为 null)。To answer your question about a "Contains" type of method, you can use the
Any
method if you want a boolean indication of its existence, or you could useFirstOrDefault
to find the firstEngineMeasurement
occurrence that satisfies the condition. If it exists it will return the actual object, otherwise it would return the default value of that object (null in this case).