LINQ GroupBy Query Operator

By Peter Bromberg

The GroupBy operator groups the elements of a sequence.

var processInfoByThreadCount =
                from process in Process.GetProcesses()
                    group process by process.Threads.Count into g
                    orderby g.Key descending
                    select new { ThreadCount = g.Key,
                                 ProcessCount = g.Count(),
                                 ThreadSum = g.Sum( p => p.Threads.Count )
                               };



             foreach (var item in processInfoByThreadCount)
            {
               Debug.WriteLine(item);
             }

LINQ GroupBy Query Operator  (1058 Views)