c# - Linq compare 2 lists of arrays -


this question has answer here:

i compare 2 lists of arrays. let's take instance example: snot duplicated don't wish compare regular lists. wish see each record in list1 how many items list2 contain how many common items.

 list<int[]> list1 = new list<int[]>() { new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 5 } };  list<int[]> list2 = new list<int[]>() { new int[2] { 1, 2 }, new int[2] { 3, 4 }, new int[2] { 3, 5 } }; 

i know each element in list1 calculate every elemet in list 2 how many common elements have.

ex. 1,2,3,4 compared 1,2 result 2 matching elements. 1,2,3,4 compared 3,5 result 1 matching element.

any ideea?

you'd this:

var results =      x in list1.select((array, index) => new { array, index })     y in list2.select((array, index) => new { array, index })     select new      {         list1_index = x.index,         list2_index = y.index,         count = x.array.intersect(y.array).count()     };  foreach(var r in results) {     console.writeline("({0}, {1}) have {2} item(s) in common.", r.list1_index, r.list2_index, r.count); } // (0, 0) have 2 item(s) in common. // (0, 1) have 2 item(s) in common. // (0, 2) have 1 item(s) in common. // (1, 0) have 2 item(s) in common. // (1, 1) have 1 item(s) in common. // (1, 2) have 2 item(s) in common. 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -