Solved

find more then min on LIST<T> or array

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applicationsā€¦
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the reā€¦
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

11 Experts available now in Live!

Get 1:1 Help Now