Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get list item that has the lowest value 3

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
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
 
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:
[COLOR=#0000FF]using[/color] System.Collections.Generic;
[COLOR=#0000FF]using[/color] System.Linq;
 
[COLOR=#0000FF]namespace[/color] ConsoleApplication1
{
    [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]Program[/color]
    {
        [COLOR=#0000FF]static[/color] [COLOR=#0000FF]void[/color] Main([COLOR=#0000FF]string[/color]&#91;&#93; args)
        {
            [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]Car[/color]&gt; Cars = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]List[/color]&lt;[COLOR=#2B91AF]Car[/color]&gt;(){
                [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Car[/color]() {Name = [COLOR=#A31515]&quot;Toyota&quot;[/color], Year = 2005, Amount = 500000, Color = [COLOR=#A31515]&quot;White&quot;[/color], Status = [COLOR=#A31515]&quot;good&quot;[/color]},
                [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Car[/color]() {Name = [COLOR=#A31515]&quot;Honda&quot;[/color], Year = 2004, Amount = 550000, Color = [COLOR=#A31515]&quot;Black&quot;[/color], Status = [COLOR=#A31515]&quot;fine&quot;[/color]},
                [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Car[/color]() {Name = [COLOR=#A31515]&quot;Nissen&quot;[/color], Year = 2012, Amount = 490000, Color = [COLOR=#A31515]&quot;Yellow&quot;[/color], Status = [COLOR=#A31515]&quot;best&quot;[/color]},
                [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Car[/color]() {Name = [COLOR=#A31515]&quot;Suzuki&quot;[/color], Year = 2012, Amount = 390000, Color = [COLOR=#A31515]&quot;Blue&quot;[/color], Status = [COLOR=#A31515]&quot;fine&quot;[/color]},
                [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Car[/color]() {Name = [COLOR=#A31515]&quot;BMW&quot;[/color], Year = 2012, Amount = 1000000, Color = [COLOR=#A31515]&quot;Green&quot;[/color], Status = [COLOR=#A31515]&quot;Good&quot;[/color]}
            };
 
            [COLOR=#0000FF]var[/color] min = Cars.Min(c =&gt; c.Amount);
            [COLOR=#0000FF]var[/color] minCars = Cars.Where(c =&gt; c.Amount == min); [COLOR=#008000]//will get a list of all cars with the minimum value[/color]
            [COLOR=#008000]//if you only want the first one, you could then do something like:[/color]
            [COLOR=#0000FF]var[/color] firstMinCar = minCars.First(); [COLOR=#008000]//ordefault will give you a null if there ins&#39;t anything there[/color]
        }
 
        [COLOR=#0000FF]public[/color] [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]Car[/color]
        {
            [COLOR=#0000FF]public[/color] [COLOR=#0000FF]string[/color] Name { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
            [COLOR=#0000FF]public[/color] [COLOR=#0000FF]int[/color] Year { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
            [COLOR=#0000FF]public[/color] [COLOR=#0000FF]int[/color] Amount { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
            [COLOR=#0000FF]public[/color] [COLOR=#0000FF]string[/color] Color { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
            [COLOR=#0000FF]public[/color] [COLOR=#0000FF]string[/color] Status { [COLOR=#0000FF]get[/color]; [COLOR=#0000FF]set[/color]; }
        }
    }
}
 
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
 
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));
 
@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:
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
 
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.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top