0

I have a two classes:

public class Profile 
{
    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; }
}

What I do is populating those two classes with data I get from XML:

This is XML structure:

<start>
    <level Current="2">
        <GPSSpots>
            <GPSSpot id="1" x="78492.61" y="-80973.03" z="-4403.297" Distance="90"/>
            <GPSSpot id="2" x="78323.57" y="-81994.98" z="-4385.707" Distance="55"/>
            <GPSSpot id="3" x="78250.57" y="-81994.98" z="-4385.707" Distance="43"/>
        </GPSSpots>
        <Vendor id = "1" x="123456" y="456789" z="0234324"/>
        <Banker id = "1" x="23432" y="3243243" z="5154445"/>
        <Relocate>
            <Teleporter id = "1" MapID="1122" x="324324" y="23432" z="23432432"/>
            <Teleporter id = "2" MapID="4444" x="324324" y="23432" z="23432432"/>           
            <Teleporter id = "3" MapID="2222" x="324324" y="23432" z="23432432"/>
        </Relocate>
</start>

And my XML parser method:

public static List<Profile> ParseXml() //Parsing all them GPSSpots from XML
{
    List<Profile> result = new List<Profile>(); //first we have to empty the list in case spawn will be called more than once
    result.Clear();

    doc = XDocument.Load(Document);

    result = (from n in doc.Descendants("level")
              select new Profile()
              {
                  Current = int.Parse(n.Attribute("Current").Value),
                  Location = (from l in n.Element("GPSSpots").Elements("GPSSpot")
                              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;
}

What I want to do now is:

I need to get closest number (value of Current from `Profile class) to let's say number 10.

Example 1 If I have Numbers on list 1,4,7,12,20 correct result should be 7.

Example 28 If I have Numbers on list 1,21,22,44 correct result should be 1.

I have managed that with following code:

var ProperLevel = GPSSpots.Where(s => s.Current < 10).Max(s => s.Current);

Now that I have that I will have to get corresponding data from class Location like: Pull node with Current value we got from from class Profile and retreive ID from Location which has lowest Distance.

Example: Let say our var ProperLevel returns 2 (like in our XML which has only one node). Now we have to pull correct node from Class Locations (corresponding var ProperLevel). Now that we have the data we sort it based on lowest Distance and return ID of.

So based on our XML result should be:

Current level = 2
ID = 3
Distance = 43
Craig W.
  • 16,585
  • 6
  • 44
  • 77
Tagyoureit
  • 315
  • 1
  • 5
  • 14

2 Answers2

1

There are some options you can choose to get object having maximum/minimum of certain property value :

How to use LINQ to select object with minimum or maximum property value

One of the shortest option available is using Aggregate() :

var ProperLevel = GPSSpots.Where(s => s.Current < 10)
                          .Aggregate((c, d) => c.Current > d.Current ? c : d);
var loc = ProperLevel.Location
                     .Aggregate((c, d) => c.Distance < d.Distance ? c : d);

Console.WriteLine("Current level = {0}", ProperLevel.Current);
Console.WriteLine("ID = {0}", loc.id);
Console.WriteLine("Distance = {0}", loc.Distance);
Community
  • 1
  • 1
har07
  • 83,990
  • 12
  • 70
  • 116
0
var loc = ProperLevel.Locations.Min(l=>l.Distance);

loc.Id...
loc.Distance
Craig W.
  • 16,585
  • 6
  • 44
  • 77
SKG
  • 152
  • 5