Link to home
Start Free TrialLog in
Avatar of center1010
center1010

asked on

find more then min on LIST<T> or array

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MinEx4()
{
    Pet[] pets = { new Pet { Name="Barley",ID=1 , Age=8 },
                   new Pet { Name="Boots",ID=2, Age=4 },
                   new Pet { Name="Whiskers",ID=3, Age=1 } };

    int min = pets.Min(pet => pet.Age);

    Console.WriteLine("The youngest is age {0}.", min);
}

Open in new window


I want to know   what is The Name and ID of youngest?please help?
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

The MIN(Of Pets) should be returning a Pet object. From the example posted on Microsoft's website for MIN, they have their Pets class implementing IComparable.
Avatar of Kimputer
Kimputer

For now, from a basic programming standpoint:

foreach (Pet pet in pets)
            {
                if (pet.Age == min)
                {
                    Console.WriteLine("Pet name is {0}, ID is {1}.", pet.Name, pet.ID  );
                }
            }

Open in new window

Hi,

First of all, you dont have ID property in your Pet class.
Just check with this code.

Add an ID property to your Pet class or remove ID from where you create a pets array.

Pet min = pets.Min();

You get the Pet object. Now access
min.Age and min.Name wherever you require.
ASKER CERTIFIED SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial