LINQ索引特定条目

Mik*_*ebb 19 c# linq indexof

我有一个MVC3 C#.Net网络应用程序.我有下面的字符串数组.

    public static string[] HeaderNamesWbs = new[]
                                       {
                                          WBS_NUMBER,
                                          BOE_TITLE,
                                          SOW_DESCRIPTION,
                                          HARRIS_WIN_THEME,
                                          COST_BOGEY
                                       };
Run Code Online (Sandbox Code Playgroud)

我想在另一个循环中找到给定条目的索引.我以为列表会有一个IndexOf.我找不到它.有任何想法吗?

Jon*_*eet 47

那么你可以使用Array.IndexOf:

int index = Array.IndexOf(HeaderNamesWbs, someValue);
Run Code Online (Sandbox Code Playgroud)

或者只是声明HeaderNamesWbsIList<string>- 如果你想要,它仍然可以是一个数组:

public static IList<string> HeaderNamesWbs = new[] { ... };
Run Code Online (Sandbox Code Playgroud)

请注意,我public static甚至不鼓励你将数组暴露出来public static readonly.你应该考虑ReadOnlyCollection:

public static readonly ReadOnlyCollection<string> HeaderNamesWbs =
    new List<string> { ... }.AsReadOnly();
Run Code Online (Sandbox Code Playgroud)

如果你想要这个IEnumerable<T>,你可以使用:

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

(+1和-1是为了"丢失"而不是0,它将返回-1)

  • `indexOf = collection.SelectMany((value, index) =&gt; value == targetValue ? new [] { index } : Enumerable.Empty&lt;int&gt;()).DefaultIfEmpty(-1).First()` (2认同)
  • @ImrePühvel:虽然我同意这是一个全新的 API,但我认为让 *this* 索引操作的行为与框架中的所有其他 `IndexOf` 方法完全相同是合理的。 (2认同)

Pau*_*aul 15

我迟到了.但我想分享我的解决方案.Jon很棒,但我更喜欢简单的lambdas.

您可以扩展LINQ本身以获得您想要的.这很简单.这将允许您使用如下语法:

// Gets the index of the customer with the Id of 16.
var index = Customers.IndexOf(cust => cust.Id == 16);
Run Code Online (Sandbox Code Playgroud)

默认情况下,这可能不是LINQ的一部分,因为它需要枚举.它不仅仅是另一个延迟选择器/谓词.

另请注意,这仅返回第一个索引.如果你想要索引(复数),你应该在方法中返回一个IEnumerable<int>yeild return index.当然不要返回-1.如果您没有按主键过滤,那将非常有用.

  public static int IndexOf<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) {

     var index = 0;
     foreach (var item in source) {
        if (predicate.Invoke(item)) {
           return index;
        }
        index++;
     }

     return -1;
  }
Run Code Online (Sandbox Code Playgroud)


Shi*_*hiv 9

如果要使用函数搜索 List 而不是指定项目值,可以使用 List.FindIndex(Predicate match)。

请参阅https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.findindex?view=netframework-4.8


sll*_*sll 6

右边List有IndexOf(),只是声明它ILIst<string>而不是string[]

public static IList<string> HeaderNamesWbs = new List<string>
                                   {
                                      WBS_NUMBER,
                                      BOE_TITLE,
                                      SOW_DESCRIPTION,
                                      HARRIS_WIN_THEME,
                                      COST_BOGEY
                                   };

int index = HeaderNamesWbs.IndexOf(WBS_NUMBER);
Run Code Online (Sandbox Code Playgroud)

MSDN:List(Of T).IndexOf方法(T)