1

Possible Duplicate:
Find sequence in IEnumerable<T> using Linq

There is a string method: int IndexOf(string value).
I failed to find a more generic one in Linq, something should look like this:

static int IndexOf<T>(this List<T> source,List<T> value,Predicate<T> equality)

Why Microsoft didn't provide us a generic string search function?

Community
  • 1
  • 1
TomCaps
  • 2,497
  • 3
  • 22
  • 25

2 Answers2

0

It sounds like your referring to List<T>.FindIndex().

--EDIT--

A more general method for any IEnumerable<T> would be:-

public static int FindIndex<T>(this IEnumerable<T> source, Predicate<T> equality)
{
    return source
        .Select((item, index) => new {Item = item, Index = index})
        .First(x => equality(x.Item)).Index;
}
Adam Ralph
  • 29,453
  • 4
  • 60
  • 67
0

Not a direct answer to your question, but in some cases you do not need to find the index and you can use a function with an index. i think my explanation does not make it any simpler, but maybe an example will make it clearer.

list.Select( (item, index) => /* do something here based on the index of the item */);
list.Where( (item, index) => /* filter the list based on the index and the item */);
Ruben
  • 6,367
  • 1
  • 24
  • 35