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);
}
I want to know what is The Name and ID of youngest?please help?
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.
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 );
}
}
Hi,
First of all, you dont have ID property in your Pet class.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.