var item = list.Where(t => somecondition);
I would love to be able to find out the index of the element that was returned, in fact, in my case all I want is an index, so that I can .Skip() into the list by that much.
Is there a way to do this in an IEnumerable? I'd hate to use a List<T>
for this, but that does have a FindIndex()
method
LINQ does not have an IndexOf method. So to find out index of a specific item we need to use FindIndex as int index = List. FindIndex(your condition); 0.
C# Linq First() Method Use the First() method to get the first element from an array. Firstly, set an array. int[] arr = {20, 40, 60, 80 , 100}; Now, use the Queryable First() method to return the first element.
Find(Predicate<T>) Method is used to search for an element which matches the conditions defined by the specified predicate and it returns the first occurrence of that element within the entire List<T>.
Use the FirstorDefault() method to return the first element of a sequence or a default value if element isn't there. List<double> val = new List<double> { }; Now, we cannot display the first element, since it is an empty collection.
If you really just need the first index then count the ones that don't match:
var index = list.TakeWhile(t => !someCondition).Count()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With