how can I select the object with the lower value with LINQ?

I have to select the object in the list with variable called “F” minor. How can I do with LINQ?
I have tried so it gives me a float, I must return the item

class Node
{

     public float G { get; set; }
     public float H { get; set; }
     public float F { get { return this.G + this.H; } } 
     public bool isWalkable { get; set; }
}

class Main
{
   List<Node> openList = new List<Node>();

   float min = openList.Min(p => p.F);

}

You may want to avoid linq for this since linq can generate a lot of garbage.

But your min doesn’t work becuase Min returns the minimum value, not the minimum item. If you want what you’re looking for with linq you can do:

var min = openList.OrderBy(p => p.F).FirstOrDefault()

But it’d be faster, and less garbage, to do this:

Node min = openList[0];
for(int i = 1; i < openList.Count; i++)
{
    if (openList[i].F < min.F) min = openList[i];
}
2 Likes

thank you very much for aid you are giving me