LINQ分组和计数

本文关键字:LINQ | 更新日期: 2023-09-27 18:26:08

我知道这个问题和我以前的问题相似,但我想不通。

数据:

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

寻找这个输出:

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

我以为这会奏效:

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

两个错误都适用于:count = g.Count(x => x.Status)

错误1无法将lambda表达式转换为委托类型"System.Func",因为块中的某些返回类型无法隐式转换为委托返回类型

错误2无法将类型"string"隐式转换为"bool"

谢谢你的耐心和帮助。

LINQ分组和计数

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