Linq query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    Linq query

    Hello!

    I have a collection with some fields we can just assume that
    we have the follwing two fields
    Parameter Value
    cc 4
    cc 4
    cc 5
    cc 5
    cc 5
    cc 6
    cc 40
    cc 40
    cc 40
    bc 7
    bc 7

    In the quesy I want to group by on Parameter = cc
    so I want to get the number of rows where each unique number occur for
    parameter =cc
    In this example I want the following output
    cc 4 2 // Here I know that I have two rows with value 4
    cc 5 3 // Here I know that I have 3 rows with value 5
    cc 6 1 // Here I know that I have 1 row with value 6
    cc 40 3 // Here I know that I have 3 rows with value 40

    //Tony

  • Marc Gravell

    #2
    Re: Linq query

    Sorry - I misread; since you only want "cc" rows, you need a "where",
    and only a simple (not composite) group:

    var qry = from item in data
    where item.Foo == "cc"
    group item by item.Bar into grp
    select new {Bar = grp.Key, Count = grp.Count()};
    foreach (var row in qry)
    {
    Console.WriteLi ne("{0}\t{1}", row.Bar, row.Count);
    }

    Marc
    [C# MVP]

    Comment

    Working...