Get List<> element position in c# using LINQ

I have a List with numbers, and I'd like to find the position of the minimum (not value) using LINQ

Example:

var lst = new List<int>() { 3, 1, 0, 5 };

Now I am looking for a function returning me

output = 2

because the minimum is at position 2 in the list.


Solution 1:

var list = new List<int> { 3, 1, 0, 5 };
int pos = list.IndexOf(list.Min()); // returns 2

Solution 2:

As you specifically asked for a LINQ solution, and all you got was non-LINQ solutions, here's a LINQ solution:

List<int> values = new List<int> { 3, 1, 0, 5 };

int index =
   values
   .Select((n, i) => new { Value = n, Index = i })
   .OrderBy(n=>n.Value)
   .First()
   .Index;

That however doesn't mean that LINQ is the best solution for this problem...

Edit:

With a bit more complex code this performs a little better:

int index =
   values
   .Select((n, i) => new { Value = n, Index = i })
   .Aggregate((a,b) => a.Value < b.Value ? a : b)
   .Index;

To get the best performance, you would use a plain loop go get through the items, while you keep track of the lowest:

int index = 0, value = values[0];
for (int i = 1; i < values.Length; i++) {
  if (values[i] < value) {
    value = values[i];
    index = i;
  }
}

Solution 3:

The best way to catch the position is by FindIndex This function is available only for List<>

Example

int id = listMyObject.FindIndex(x => x.Id == 15); 

If you have enumerator or array use this way

int id = myEnumerator.ToList().FindIndex(x => x.Id == 15); 

or

   int id = myArray.ToList().FindIndex(x => x.Id == 15); 

Solution 4:

I agree that LINQ isn't the best solution for this problem, but here's another variation that is O(n). It doesn't sort and only traverses the list once.

var list = new List<int> { 3, 1, 0, 5 };
int pos = Enumerable.Range(0, list.Count)
    .Aggregate((a, b) => (list[a] < list[b]) ? a : b); // returns 2

Solution 5:

var data = new List<int> { 3, 1, 0, 5 };

var result = Enumerable.Range(0, data.Count).OrderBy(n => data[n]).First();