tags:

views:

110

answers:

1

I have a matrix, IEnumerable<IEnumerable<int>> matrix, for example:

{ {10,23,16,20,2,4},  {22,13,1,33,21,11 }, {7,19,31,12,6,22}, ... }

and another array:

int[] arr={ 10, 23, 16, 20}

I want to filter the matrix on the condition that I group all rows of the matrix which contain the same number of elements from arr.

That is to say the first row in the matrix {10,23,16,20,2,4} has 4 numbers from arr, this array should be grouped with the rest of the rows with 4 numbers from arr.

better to use linq, thank you very much!

A: 

This worked for me:

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},
};

Output:

Lists that have 0 of the numbers:
{ 22, 13, 1, 33, 21, 11 }
{ 7, 19, 31, 12, 6, 22 }
Lists that have 1 of the numbers:
{ 10, 13, 31, 12, 6, 22 }
{ 10 }
Lists that have 3 of the numbers:
{ 10, 19, 20, 16, 6, 22 }
{ 10, 13, 16, 20 }
Lists that have 4 of the numbers:
{ 10, 23, 16, 20, 2, 4 }

BlueRaja - Danny Pflughoeft
For extra credit, change `lists` to `lists.AsParallel()` in the LINQ-query - **BAM!** Instant multithreading! (VS 2010 only)
BlueRaja - Danny Pflughoeft
First thank your outstanding help, but in fact the int[] searchNums also is a matrix, like int[][] cj= { { 10, 23, 16, 20 }, { 22, 13, 1, 33 }, { 7, 19, 31, 12 }}, how should I do?Thank you!!
Bob Feng
Well if the question is the same, just combine that into a single-dimensional array beforehand...
BlueRaja - Danny Pflughoeft
Thank you, But according to query requirement, the matrix searchNums can not be combined to a single-dimensional array. If combined, I couldn't analyze the result matrix lists from the every little array. Based on the little array this is just what I want.Please take a look at this question. thank you!
Bob Feng
Hi BlueRaja, thank you for your help, the question has already been resolved. I write a extension method:public static int Amount<T>(this IEnumerable<T> source, IEnumerable<IEnumerable<T>> baseElements) { int ret=0; var sum = from m in baseElements group m by m.Count(num =>source.Contains(num)) into g orderby g.Key select g; return ret; }. be used to count. Thanks again for your help!!
Bob Feng