-4

I want to return the User where the amount is the lowest.

List<User> users = new List<User>();
users.Add(new User("Patrick", 39m));
users.Add(new User("Claude", 13.7m));
users.Add(new User("Steven", -45.3m));    

Decimal lowest = users.Min(user => user.Amount); //this gives me -45.3m

However what I need is not -45.3m, I need User{"Steven", -45.3m}

Happy Bird
  • 1,012
  • 2
  • 11
  • 29
  • 1
    Sort by amount and take the first object in the new collection. – Michael McGriff Jun 21 '17 at 19:08
  • @MichaelMcGriff yes that's what I have done already and this works perfectly, but I want to write it cleaner like the example I give with Decimal. – Happy Bird Jun 21 '17 at 19:11
  • 5
    What did you search for? I googled "linq select lowest object in list" and this is the first result: https://stackoverflow.com/questions/914109/how-to-use-linq-to-select-object-with-minimum-or-maximum-property-value – Jack Marchetti Jun 21 '17 at 19:11

1 Answers1

1

What about something like this... just loop through the list and find the smallest amount.

User lowest = users[0];
foreach(User u in users){
    if(u.Amount < lowest.Amount)
       lowest = u;
}
phishfordead
  • 377
  • 2
  • 7
  • The link Jack Marchetti provided in your original question should get you on the right track.. I just added this answer here as an alternative way to go about it.. And if people want to down vote me, it's really no sweat off my back. I'm just out trying to help. – phishfordead Jun 21 '17 at 19:21