Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding minimum values of (properties of ) collections in C#

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")?

like image 216
Dave Avatar asked Sep 29 '10 22:09

Dave


3 Answers

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

like image 164
Dan Abramov Avatar answered Oct 02 '22 15:10

Dan Abramov


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.

like image 30
Abe Miessler Avatar answered Oct 02 '22 15:10

Abe Miessler


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);
like image 45
Ahmad Mageed Avatar answered Oct 02 '22 15:10

Ahmad Mageed