KTLind
KTLind

Reputation: 147

LINQ grouping and counting

I know this question is similar to a previous one of mine but I can't figure this out.

data:

dept  needOn    status
foo   5/1/2011  closed
foo   5/1/2011  closed
foo   5/1/2011  closed
foo   5/1/2011  closed
foo   5/1/2011  open
foo   5/1/2011  open

looking for this output:

dept  needOn    status   count
foo   5/1/2011  closed   4
foo   5/1/2011  open     2

I thought this would work:

var query3 = from q3 in query2
             group new { q3.Status }
                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(x => x.Status)
             };

Both errors apply to: count = g.Count(x => x.Status)

Error 1 Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type

Error 2 Cannot implicitly convert type 'string' to 'bool'

Thanks for your patience and help.

Upvotes: 0

Views: 82

Answers (1)

AD.Net
AD.Net

Reputation: 13399

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()
             };

Upvotes: 2

Related Questions