在 C# 中查找集合(的属性)的最小值

发布于 2024-09-25 15:20:28 字数 1412 浏览 5 评论 0原文

给出来自 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 技术交流群。

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

发布评论

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

评论(3

ぺ禁宫浮华殁 2024-10-02 15:20:28
using System.Linq;

var collection = new EngineMeasurementCollection();
int maxSpeed = collection.Max(em => em.Speed);

另请参阅:
LINQ MSDN 文档
LINQ to Objects 5 分钟概述

using System.Linq;

var collection = new EngineMeasurementCollection();
int maxSpeed = collection.Max(em => em.Speed);

See also:
LINQ MSDN documentation
LINQ to Objects 5 Minute Overview

纵山崖 2024-10-02 15:20:28

添加到 gaearon 的答案:

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

将为您提供最低限度。但您可能会自己弄清楚;)

您可以查看 MSDN 网站上的此链接 详细介绍了使用 linq 查找最大值/最小值。

To add to gaearon's answer:

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

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.

堇年纸鸢 2024-10-02 15:20:28

要回答有关“Contains”类型方法的问题,如果您需要布尔值指示其是否存在,则可以使用 Any 方法,或者可以使用 FirstOrDefault 来查找第一个满足条件的 EngineMeasurement 出现。如果存在,它将返回实际对象,否则将返回该对象的默认值(在本例中为 null)。

bool result = collection.Any(m => m.Speed == 2000); // true

// or

var em = collection.FirstOrDefault(m => m.Speed == 2000);
if (em != null)
    Console.WriteLine("Torque: {0}, Speed: {1}", em.Torque, em.Speed);

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 use FirstOrDefault to find the first EngineMeasurement 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).

bool result = collection.Any(m => m.Speed == 2000); // true

// or

var em = collection.FirstOrDefault(m => m.Speed == 2000);
if (em != null)
    Console.WriteLine("Torque: {0}, Speed: {1}", em.Torque, em.Speed);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文