Find array index in array list in C #

If you have a list of arrays like:

List<int[]> ListOfArrays = new List<int[]>(); ListOfArrays.Add(new int[] { 1, 1 }); ListOfArrays.Add(new int[] { 2, 1 }); 

How do you find the index {2, 1} in the list?

I do not want to use an iteration loop. I would like to use a compressed method similar to what PaRiMaL RaJ suggested in Check if a string array exists in a list of strings :

 list.Select(ar2 => arr.All(ar2.Contains)).FirstOrDefault(); 

(the above code will return true if the members of the given array of strings exist in the list of string arrays)

+6
source share
3 answers
 var myArr = new int[] { 2, 1 }; List<int[]> ListOfArrays = new List<int[]>(); ListOfArrays.Add(new int[] { 1, 1 }); ListOfArrays.Add(new int[] { 4, 1 }); ListOfArrays.Add(new int[] { 1, 1 }); ListOfArrays.Add(new int[] { 2, 1 }); int index = ListOfArrays.FindIndex(l => Enumerable.SequenceEqual(myArr, l)); 
+8
source

You can use the SequenceEqual method for this. See MSDN: https://msdn.microsoft.com/en-us/library/vstudio/bb348567(v=vs.100).aspx

+1
source

You can try the following:

 List<int[]> ListOfArrays = new List<int[]>(); ListOfArrays.Add(new int[] { 1, 1 }); ListOfArrays.Add(new int[] { 2, 1 }); var chk = ListOfArrays.FindIndex(e => (e.SequenceEqual(new int[] { 2, 1 }))); 
+1
source

All Articles