INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

How to get list item that has the lowest value

How to get list item that has the lowest value

(OP)
If I have the following list, how to I get the item (not the value) that has the smallest amount?

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

RE: How to get list item that has the lowest value

Once you have the minimum value, you could do a fairly easy Where to get the items that match that minimum value. You might have more than one result, so you will have to decide what to use as precedence after amount. Something like below should work for you. Just remember to do error checking - like what happens if the list is empty for some reason?

CODE

using System.Collections.Generic;
using System.Linq;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            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"}
            };
 
            var min = Cars.Min(c => c.Amount);
            var minCars = Cars.Where(c => c.Amount == min); //will get a list of all cars with the minimum value
            //if you only want the first one, you could then do something like:
            var firstMinCar = minCars.First(); //ordefault will give you a null if there ins't anything there
        }
 
        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; }
        }
    }
} 

RE: How to get list item that has the lowest value

(OP)
Looks good.

I was looking at finding different ways to do the same thing and this is a little clearer than using 2 linqs.

Thanks,

Tom

RE: How to get list item that has the lowest value

I've not used Linq, but am interested in getting to know how it works.

As such I have question based on the above answer:

Would the following be possible?

CODE

var minCars = Cars.Where(c => c.Amount == Cars.Min(c => c.Amount)); 

RE: How to get list item that has the lowest value

@softhemc

Yes that will work but with one small change. You just need to change the second lambda variable from 'c' to something else as 'c' is already defined.

CODE --> c#

var minCars = Cars.Where(c => c.Amount == Cars.Min(d => d.Amount)); 

Do you want some custom SIM scripts developed. Contact me via my website http://www.microspostools.com/

RE: How to get list item that has the lowest value

That will work fine as Cathal says, I just tend not to like embedding linq within linq. When you start getting into more advanced things you can do with linq, it gets confusing fast. It doesn't change the performance given you're still calling two separate functions, so for readability I usually separate them out.

RE: How to get list item that has the lowest value

That's great to know - thanks both for your help and advice. I mainly program in VB and tend to go for the "one-liners" where possible.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close