Solved

find more then min on LIST<T> or array

Posted on 2015-02-12
5
56 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 15

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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