IndicesOf
Finds all the indexes of the give value or values in an enumerable list
Source
public static IEnumerable<int> IndicesOf<T>(this IEnumerable<T> obj, T value)
{
return (from i in Enumerable.Range(0, obj.Count())
where obj.ElementAt(i).Equals(value)
select i);
}
public static IEnumerable<int> IndicesOf<T>(this IEnumerable<T> obj, IEnumerable<T> value)
{
return (from i in Enumerable.Range(0, obj.Count())
where value.Contains(obj.ElementAt(i))
select i);
}
Example
var t = new Collection<int> { 0,1,2,3,1,1,3,4 };
var first = t.IndicesOf(3);
//returns 3,6
var second = t.IndicesOf(new int[]{0,3});
//returns 0,3,6
Author: Daniel Gidman
Submitted on: 3 dec. 2010
Language: C#
Type: System.Collections.Generic.IEnumerable<T>
Views: 5059