Skip to main content

Posts

Showing posts from October, 2016

Grouping and joining in LINQ

Grouping and joining in LINQ foreach ( var line in data . GroupBy ( info => info . metric ) . Select ( group => new { Metric = group . Key , Count = group . Count () }) . OrderBy ( x => x . Metric ) dept needOn status foo 5 / 1 / 2011 closed foo 5 / 1 / 2011 closed foo 5 / 1 / 2011 open foo 5 / 1 / 2011 open Output Required dept needOn status count foo 5 / 1 / 2011 closed 4 foo 5 / 1 / 2011 open 2 Solution: var query3 = from q3 in query2 group q3 by new { q3 . Dept , q3 . NeedOnWeek , q3 . Status } into g select new { dept = g . Key . Dept , needOnWeek = g . Key . NeedOnWeek , status = g . Key . Status , count = g . Count () ...