Say I have the following list:
List<int> integerList = new List<int> {
1, 10, 13, 5, 7, 123, 47, 69, 22, 77, 94, 201, 120, 73, 98, 99, 101, 4, 6, 9, 19, 21, 24, 221, 909, 45, 27, 28, 29, 30,
};
What Linq query will retrieve for me the index of a certain value in the list? In on other words, I’d like a query that would return to me an index value of 5 if I pass it a value of ‘123’.
Thank you.
Well the easiest approach is not to use LINQ 🙂
However, if you must:
That will give you an
int?with a “null” value if the value can’t be found, or the first matching index otherwise. You can turn it into the regular “use -1 as not found” using the null coalescing operator: