1

In SQL this would be easy for me...

SELECT browser, browser_version, page_name, COUNT(*) as cnt 
FROM traffic
GROUP BY browser, browser_version, page_name

This would return one row for each unique combination of browser, browser_version, page_name, along with the count of duplicates.

I'd like to do the same thing in Linq, but I'm not sure how to do it.

By the way, I'm not using SQL in this case because the data didn't come from SQL. It came from Windows Azure Table Storage. And I'm working with the data in-memory.

Steve Wortham
  • 21,740
  • 5
  • 68
  • 90

2 Answers2

2
var query =
    from t in traffic
    group t by new { t.browser, t.browser_version, t.page_name } into g
    select new
    {
        g.Key.browser,
        g.Key.browser_version,
        g.Key.page_name,
        Count = g.Count()
    }
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Thanks man. My problem was a little more complex than the example I gave. I also had to get the latest date for every grouping. But your solution was perfect and helped me figure it all out. – Steve Wortham Feb 11 '12 at 21:10
1

You'd use a similar method:

var results = traffic.GroupBy(t => new { t.browser, t.browser_version, t.page_name });

foreach(var group in results)
{
    Console.WriteLine("{0}/{1}/{2} has {3} items.", 
            group.Key.browser, 
            group.Key.browser_version, 
            group.Key.page_name, 
            group.Count());
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373