C# 特定条目的 LINQ 索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9300169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ indexOf a particular entry
提问by MikeTWebb
I have an MVC3 C#.Net web app. I have the below string array.
我有一个 MVC3 C#.Net Web 应用程序。我有下面的字符串数组。
public static string[] HeaderNamesWbs = new[]
{
WBS_NUMBER,
BOE_TITLE,
SOW_DESCRIPTION,
HARRIS_WIN_THEME,
COST_BOGEY
};
I want to find the Index of a given entry when in another loop. I thought the list would have an IndexOf. I can't find it. Any ideas?
我想在另一个循环中找到给定条目的索引。我认为该列表会有一个 IndexOf。我找不到。有任何想法吗?
采纳答案by Jon Skeet
Well you can use Array.IndexOf:
那么你可以使用Array.IndexOf:
int index = Array.IndexOf(HeaderNamesWbs, someValue);
Or just declare HeaderNamesWbsas an IList<string>instead - which can still be an array if you want:
或者只是声明HeaderNamesWbs为一个IList<string>- 如果需要,它仍然可以是一个数组:
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, even public static readonly。你应该考虑ReadOnlyCollection:
public static readonly ReadOnlyCollection<string> HeaderNamesWbs =
new List<string> { ... }.AsReadOnly();
If you ever want this for IEnumerable<T>, you could use:
如果你想要这个 for IEnumerable<T>,你可以使用:
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.)
(+1 和 -1 是为了“丢失”返回 -1 而不是 0。)
回答by sll
Right Listhas IndexOf(), just declare it as ILIst<string>rather than string[]
RightList有 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);
回答by Paul
I'm late to the thread here. But I wanted to share my solution to this. Jon's is awesome, but I prefer simple lambdas for everything.
我来晚了。但我想分享我对此的解决方案。Jon 很棒,但我更喜欢简单的 lambda 表达式。
You can extend LINQ itself to get what you want. It's fairly simple to do. This will allow you to use syntax like:
您可以扩展 LINQ 本身以获得您想要的。做起来相当简单。这将允许您使用如下语法:
// Gets the index of the customer with the Id of 16.
var index = Customers.IndexOf(cust => cust.Id == 16);
This is likely not part of LINQ by default because it requires enumeration. It's not just another deferred selector/predicate.
默认情况下,这可能不是 LINQ 的一部分,因为它需要枚举。它不仅仅是另一个延迟选择器/谓词。
Also, please note that this returns the first index only. If you want indexes (plural), you should return an IEnumerable<int>and yeild return indexinside the method. And of course don't return -1. That would be useful where you are not filtering by a primary key.
另外,请注意这仅返回第一个索引。如果你想要索引(复数),你应该在方法内部返回一个IEnumerable<int>and 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;
}
回答by Shiv
If you want to search List with a function rather than specifying an item value, you can use List.FindIndex(Predicate match).
如果要使用函数搜索 List 而不是指定项目值,可以使用 List.FindIndex(Predicate match)。

