none
Using LINQ to count the occurences of values in array field of objects in colection

    Question

  • I am fairly new at query lanuages like LINQ. I think this is a pretty simple task but I cannot figure it out or find an example anywhere.

    I have a collection of objects - a List. Inside the objects is an array of integers. I want to generate a histogram of all the values that occur in those arrays.

    Thanks in advance.
    Friday, December 11, 2009 4:56 AM

Answers

  • Ah, I understand LINQ at least that much. I figured out what to do:

    What I have is like this (simplified):

    class A {
        int[] Nums;
    }

    and

    List<A> myListOfA;

    I want to count occurrences over all the List.

    I didn't realize I could say

    from a in myListOfA
    from n in a.Nums

    I was thinking of the second from...in as a nested loop and I did not think the grouping would be visible outside of it.




     
    • Marked as answer by Omnoculus Saturday, December 12, 2009 4:40 AM
    Friday, December 11, 2009 5:11 PM

All replies

  • You may want to try the Linq GroupBy operator.

    Here is a link to some examples:

    http://msdn.microsoft.com/en-us/vcsharp/aa336754.aspx

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Friday, December 11, 2009 6:19 AM
  • public void GetIntegerOccurance() {

     int[] Nums = { 1, 2, 3, 4, 5, 1, 4, 5, 6, 1, 2, 3, 8, 2, 2, 2, 2 }; 

     var NumsOccr = from num in Nums 
     group num by num into g 
     select new { number = g.Key, Occurance = g.Count() }; 

     foreach (var item in NumsOccr) 
     { 
     Console.WriteLine(item.number.ToString() + " Occurs " + item.Occurance.ToString()); 
     } 

     }
    Friday, December 11, 2009 6:49 AM
  • Ah, I understand LINQ at least that much. I figured out what to do:

    What I have is like this (simplified):

    class A {
        int[] Nums;
    }

    and

    List<A> myListOfA;

    I want to count occurrences over all the List.

    I didn't realize I could say

    from a in myListOfA
    from n in a.Nums

    I was thinking of the second from...in as a nested loop and I did not think the grouping would be visible outside of it.




     
    • Marked as answer by Omnoculus Saturday, December 12, 2009 4:40 AM
    Friday, December 11, 2009 5:11 PM
  • First off where is good tutorial or book on linq, im a beginner in linq, but im coming up to speed on linq

     

    ok:  In the list call codesThatOccur also has item call testsname, I wnat to be able group by list.codes and list.testnames

          each testNames has there own individual codes that occur.

     

       the linq below only shows the the code, and how many times they occur, how to format the linq to show testname, code, and then the number occurences by testnames

     

     


                var codeThatOccr = from list in codeList.codesThatOccur
                         group list by list.codes into g
                         select new { number = g.Key, Occurance = g.Count() };

    Saturday, October 23, 2010 4:51 PM