Solved

find more then min on LIST<T> or array

Posted on 2015-02-12
5
52 Views
Last Modified: 2015-02-12
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?
0
Comment
Question by:center1010
5 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40605470
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.
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40605497
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

0
 
LVL 19

Expert Comment

by:Rikin Shah
ID: 40605538
Hi,

First of all, you dont have ID property in your Pet class.
0
 
LVL 19

Expert Comment

by:Rikin Shah
ID: 40605551
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.
0
 
LVL 14

Accepted Solution

by:
Lokesh B R earned 500 total points
ID: 40605669
Hi,

Check this,
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 } };

            Pet pet = pets.OrderBy(c => c.Age).FirstOrDefault();
            if (pet != null)
            {
                Console.WriteLine("The youngest is ID {0}.", pet.ID);
                Console.WriteLine("The youngest is Name {0}.", pet.Name);
                Console.WriteLine("The youngest is age {0}.", pet.Age);
            }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
Icons and Colors for Terms 3 24
SQL Login 17 38
C# Single Form 8 27
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now