c# retrieving values from list

Hello,

I have this classes:

 public class Profile //class for storing Locations and [Current] value
        {
            private List<Location> location = new List<Location>();
            public int Current { get; set; }
            public List<Location> Location { get; set; }
        }

        public class Location
        {
            public int id { get; set; }
            public Single x { get; set; }
            public Single y { get; set; }
            public Single z { get; set; }
            public float Distance { get; set; }
        }

Open in new window


I am populating those two classes with following method:
public static List<Profile> ParseXml() 
        {

            List<Profile> result = new List<Profile>();
            result.Clear();

            doc = XDocument.Load(Document);

            result = (from n in doc.Descendants("Current")
                      select new Profile()
                      {
                          Current = int.Parse(n.Attribute("Current").Value),
                          Location = (from l in n.Element("Locations").Elements("Location")
                                      select new Location()
                                      {
                                          id = int.Parse(l.Attribute("id").Value),
                                          x = Single.Parse(l.Attribute("x").Value),
                                          y = Single.Parse(l.Attribute("y").Value),
                                          z = Single.Parse(l.Attribute("z").Value),
										  Distance = Single.Parse(l.Attribute("Distance").Value),
                                      }).ToList()
                      }).ToList();

            return result;
        }

Open in new window


And now i want to do the following:

First i need to retrieve value from Profile class which is smallest yet biggest number on list that is still smaller than X value.
I have done that:

		var ProperID = list.Where(s => s.Current < 10).Max(s => s.Current);

Open in new window


Now that I have ProperID value I need to retrieve value from class Location that has Min(Distance).

If question is confusing please ask away and i will try to make it more clear.

PS this is my way of handing this ... if anyone has a better more simple solution i would gladly take it.

Basically what i need is this  =>
 var ProperID = list.Where(s => s.Current < 10).Max(s => s.Current); 

Open in new window

and then Min(distance) of coresponding value in class Location(it should return one value for each ProperID).

Thank you!
andy gehoxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

louisfrCommented:
Instead of getting the max, order the list by that value and take the last item.
var loc = list.Where(s => s.Current < 10).OrderBy(s => s.Current).Last().Location.Min(s => s.Distance);

Open in new window

You can also order first and then get the last item less than 10:
var loc = list.OrderBy(s => s.Current).Last(s => s.Current < 10).Location.Min(s => s.Distance);

Open in new window

Not sure which is best.
Fernando SotoRetiredCommented:
Hi Andy;

See if this gives the solution you are looking for.

Single minDist = list.Where(s => s.Current == ProperID).Min(s => s.Location.Distance);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andy gehoxAuthor Commented:
Excelent!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.