If I have the following list, how to I get the item (not the value) that has the smallest amount?
Something like:
Car c = Cars.Min(a => a.Amount);
This doesn't work because it passes back a value. I want the item with that value.
This works, but I was wondering if there was a better way than a Lambda inside of a Lambda?
Car c = Cars.Find(y => y.Amount == Cars.Min(x => x.Amount));
I would still like to use a Lambda expression but was wondering if there was one expression that is made to do this?
Thanks,
Tom
Thanks,
Tom
Code:
public class Car
{
public string Name { get; set; }
public int Year { get; set; }
public int Amount { get; set; }
public string Color { get; set; }
public string Status { get; set; }
}
List<Car> Cars = new List<Car>(){
new Car() {Name = "Toyota", Year = 2005, Amount = 500000, Color = "White", Status = "good"},
new Car() {Name = "Honda", Year = 2004, Amount = 550000, Color = "Black", Status = "fine"},
new Car() {Name = "Nissen", Year = 2012, Amount = 490000, Color = "Yellow", Status = "best"},
new Car() {Name = "Suzuki", Year = 2012, Amount = 390000, Color = "Blue", Status = "fine"},
new Car() {Name = "BMW", Year = 2012, Amount = 1000000, Color = "Green", Status = "Good"}
};
Something like:
Car c = Cars.Min(a => a.Amount);
This doesn't work because it passes back a value. I want the item with that value.
This works, but I was wondering if there was a better way than a Lambda inside of a Lambda?
Car c = Cars.Find(y => y.Amount == Cars.Min(x => x.Amount));
I would still like to use a Lambda expression but was wondering if there was one expression that is made to do this?
Thanks,
Tom
Thanks,
Tom