LINQ indexOf a particular entry

Well you can use Array.IndexOf:

int index = Array.IndexOf(HeaderNamesWbs, someValue);

Or just declare HeaderNamesWbs as an IList<string> instead – which can still be an array if you want:

public static IList<string> HeaderNamesWbs = new[] { ... };

Note that I’d discourage you from exposing an array as public static, even public static readonly. You should consider ReadOnlyCollection:

public static readonly ReadOnlyCollection<string> HeaderNamesWbs =
    new List<string> { ... }.AsReadOnly();

If you ever want this for IEnumerable<T>, you could use:

var indexOf = collection.Select((value, index) => new { value, index })
                        .Where(pair => pair.value == targetValue)
                        .Select(pair => pair.index + 1)
                        .FirstOrDefault() - 1;

(The +1 and -1 are so that it will return -1 for “missing” rather than 0.)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)