Solved

find more then min on LIST<T> or array

Posted on 2015-02-12
5
55 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 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

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

696 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