So let's say I have a type Car
with two properties Speed
and Color
public class Car
{
public int Speed {get; set;}
public string Color {get; set;}
}
Using LINQ I may find the minimum speed
int minSpeed = collection.Min(em => em.Speed);
so minSpeed will contain the value of speed of the car with the minimum speed in collection.
But how can I do something similar to get the color of the car?
Something like:
string color = collection.Min(em => em.Speed).Select(x => x.Color);
Use MinBy .
Car slowestCar = collection.MinBy(em => em.Speed);
string color = slowestCar.Color;
How about:
IEnumerable<string> color = collection.Where(x=> x.Speed == collection.Min(em => em.Speed)).Select(x => x.Color).Distinct();
Of course, you can have several cars with same minimum speed, so you get IEnumerable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.