我有一个包含整数{10, 11, 23, 34, 56, 43}的列表,我想找出所有大于23的项的索引。这些值可以是任何顺序,所以我不想对它们进行排序。
List<int> mylist = new List<int> { 10, 11, 23, 34, 56, 43 };
我对所有符合条件的项的索引感兴趣,而不仅仅是符合条件的第一项。所以这行代码对我来说不起作用。
int index = mylist.FindIndex( x => x > 23 );
var indexes = mylist.Select((v, i) => new { v, i })
.Where(x => x.v > 23)
.Select(x => x.i);
public static IEnumerable<int> FindIndices<T>(this IEnumerable<T> items, Func<T, bool> predicate)
{
int i = 0;
foreach (var item in items)
{
if (predicate(item))
{
yield return i;
}
i++;
}
}
foreach (int index in mylist.FindIndices( x => x > 23 ))
...
这种方法比上述其他方法更高效。然而,这只对大型序列有显著意义!
这个扩展方法可以很好地完成工作,而且代码简洁明了:
public static class ListExtensions
{
/// <summary>
/// Finds the indices of all objects matching the given predicate.
/// </summary>
/// <typeparam name="T">The type of objects in the list.</typeparam>
/// <param name="list">The list.</param>
/// <param name="predicate">The predicate.</param>
/// <returns>Indices of all objects matching the given predicate.</returns>
public static IEnumerable<int> FindIndices<T>(this IList<T> list, Func<T, bool> predicate)
{
return list.Where(predicate).Select(list.IndexOf);
}
}
请查看工作演示
_list.Select((v, i) => new {v, i}).Where(x => x.v.Priority > activity.Priority).Select(x => x.i).First();
- gregsdennis_list.Select((v, i) => new {v, i}).First(x => x.v.Priority > activity.Priority).i;
_list是一个列表,Select方法会将每个元素映射为一个新的对象,包含原始元素和它的索引。然后使用First方法找到第一个满足条件(v.Priority > activity.Priority)的元素,并返回它的索引i。 - Edwin StotelerSelect((v, i)...
是如何工作的吗?为什么i
包含索引? - A.Pissicat