c# Array.FindAllIndexOf which FindAll IndexOf

后端 未结 9 1921
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 08:16

I know c# has Array.FindAll and Array.IndexOf.

Is there a Array.FindAllIndexOf which returns int[]?

9条回答
  •  失恋的感觉
    2020-11-29 08:48

    I've used Nikhil Agrawal's answer to create the following related method, which may be useful.

    public static List FindAllIndexOf(List values, List matches)
        {
            // Initialize list
            List index = new List();
    
            // For each value in matches get the index and add to the list with indexes
            foreach (var match in matches)
            {
                // Find matches 
                index.AddRange(values.Select((b, i) => Equals(b, match) ? i : -1).Where(i => i != -1).ToList());
    
            }
    
            return index;
        }
    

    Which takes a list with values and a list with values that are to be matched. It returns a list of integers with the index of the matches.

提交回复
热议问题