Getting a collection of index values using a LINQ query

纵然是瞬间 提交于 2019-12-17 18:54:37

问题


Is there a better way to do this?

string[] s = {"zero", "one", "two", "three", "four", "five"};

var x = 
s
.Select((a,i) => new {Value = a, Index = i})
.Where(b => b.Value.StartsWith("t"))
.Select(c => c.Index);

i.e. I'm looking for a more efficient or more elegant way to get the positions of the items matching the criteria.


回答1:


You could easily add your own extension method:

public static IEnumerable<int> IndexesWhere<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
    int index=0;
    foreach (T element in source)
    {
        if (predicate(element))
        {
            yield return index;
        }
        index++;
    }
}

Then use it with:

string[] s = {"zero", "one", "two", "three", "four", "five"};
var x = s.IndexesWhere(t => t.StartsWith("t"));



回答2:


If you're just using the example as a way to learn LINQ, ignore this post.


It's not clear to me that LINQ is actually the best way to do this. The code below seems like it would be more efficient since no new anonymous type needs to be created. Granted, your example may be contrived and the technique might be more useful in a different context, for example in a data structure where it could take advantage of an index on value, but the code below is reasonably straight-forward, understandable (no thought required) and arguably more efficient.

string[] s = {"zero", "one", "two", "three", "four", "five"};
List<int> matchingIndices = new List<int>();

for (int i = 0; i < s.Length; ++i) 
{
   if (s[i].StartWith("t"))
   {
      matchingIndices.Add(i);
   }
}



回答3:


Seems fine to me. You might save a couple characters by changing the select to:

.Select((Value, Index) => new {Value, Index})



回答4:


There is also FindIndex method in Collection List for which you create a delete method which can return the index from the collection. you can refer to the following link in msdn http://msdn.microsoft.com/en-us/library/x1xzf2ca.aspx.




回答5:


How about this? It's similar to the original poster's but I first select the indexes and then build a collection which matches the criteria.

var x = s.Select((a, i) => i).Where(i => s[i].StartsWith("t"));

This is a tad less efficient than some of the other answers as the list is fully iterated over twice.




回答6:


I discussed this interesting problem with a colleague and at first I thought JonSkeet's solution was great, but my colleague pointed out one problem, namely that if the function is an extension to IEnumerable<T>, then it can be used where a collection implements it.

With an array, it's safe to say the order produced with foreach will be respected (i.e. foreach will iterate from first to last), but it would not necessarily be the case with other collections (List, Dictionary, etc), where foreach would not reflect necessarily "order of entry". Yet the function is there, and it can be misleading.

In the end, I ended up with something similar to tvanfosson's answer, but as an extension method, for arrays:

public static int[] GetIndexes<T>(this T[]source, Func<T, bool> predicate)
{
    List<int> matchingIndexes = new List<int>();

    for (int i = 0; i < source.Length; ++i) 
    {
        if (predicate(source[i]))
        {
            matchingIndexes.Add(i);
        }
    }
    return matchingIndexes.ToArray();
}

Here's hoping List.ToArray will respect the order for the last operation...



来源:https://stackoverflow.com/questions/237377/getting-a-collection-of-index-values-using-a-linq-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!