我有一个矩阵IEnumerable<IEnumerable<int>>
矩阵,例如:
{ {10,23,16,20,2,4}, {22,13,1,33,21,11 }, {7,19,31,12,6,22}, ... }
和另一个数组:
int[] arr={ 10, 23, 16, 20}
我想过滤矩阵,条件是我将矩阵的所有行分组,这些行包含来自arr的相同数量的元素。
也就是说矩阵{10,23,16,20,2,4}
中的第一行有4个来自arr的数字,这个数组应该与其余的行组成,其中4个数字来自arr。
最好使用linq,非常感谢!
答案 0 :(得分:0)
这对我有用:
private static void Main(string[] args)
{
int[] searchNums = new int[] {10, 23, 16, 20};
var groupByCount = from o in lists
group o by o.Count(num => searchNums.Contains(num)) into g
orderby g.Key
select g;
foreach(var grouping in groupByCount)
{
int countSearchNums = grouping.Key;
Console.WriteLine("Lists that have " + countSearchNums + " of the numbers:");
foreach(IEnumerable<int> list in grouping)
{
Console.WriteLine("{{ {0} }}", String.Join(", ", list.Select(o => o.ToString()).ToArray()));
}
}
Console.ReadKey();
}
private static List<List<int>> lists = new List<List<int>>
{
new List<int> {10, 23, 16, 20, 2, 4},
new List<int> {22, 13, 1, 33, 21, 11},
new List<int> {7, 19, 31, 12, 6, 22},
new List<int> {10, 13, 31, 12, 6, 22},
new List<int> {10, 19, 20, 16, 6, 22},
new List<int> {10},
new List<int> {10, 13, 16, 20},
};
输出:
列表中包含0个数字:
{22,13,1,33,21,11}
{7,19,31,12,6,22}
列表中包含1个数字:
{10,13,31,12,6,22}
{10}
列表中有3个数字:
{10,19,20,16,6,22}
{10,13,16,20} 列表中有4个数字:
{10,23,16,20,2,4}