Finding minimum values ​​(properties) of sets 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 to get minimum / maximum speed or torque or power. I need this to set the scale on the chart I'm doing (more precisely, the WPF Toolkit Chart). I suppose I might have a method inside the EngineMeasurementCollection that iterates through each EngineMeasurement and looks at Power (or Speed), but I suspect there is a much simpler way? The Collection class has some kind of Min method, but note, I'm not trying to get the minimum of the collection (I'm not sure what this will mean in this case), but rather the minimum of a certain property (speed). I have seen using Collection.Min with functors. Is there something you could do there? Or with Link? I'm interested in all the ways. Thanks, Dave

Bonus question (maybe this will be obvious to me with the answer to min / max). What are the options to determine if a value is (for example, Speed ​​is already in the collection). This is not clear from this example, but it may be that if you already have data for a given independent variable (e.g. time), you no longer need to. So, is there something like Collection.Contains ("indicate property of interest to you")?

+4
source share
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 Review

+12
source

To add gaireon to the answer:

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

You will get a minimum. But you would probably think about it yourself;)

You can take a look at this link on the MSDN website , which browses the search for max / min values ​​using linq.

+2
source

To answer a question about a method of type Contains, you can use the Any method if you want to have a logical indication of its existence, or you can use FirstOrDefault to find the first EngineMeasurement event that satisfies the condition. If it exists, it will return the actual object, otherwise it will return the default value of that object (in this case 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); 
+2
source

All Articles