Linq- Select 带计数的列

Linq- Select Column with count

我需要一个简单的 Linq 查询来获取员工记录以及来自外键的计数 table(部门)。以下查询无效

System.InvalidOperationException: 'The LINQ expression 'GroupByShaperExpression: KeySelector: q.DeptId , ElementSelector:new ................ .Select(x => x.EmpName) .First()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

var query = from h in context.Employee
                        join p in context.Dept on h.EmpId equals p.DeptId
                        select new
                        {
                            h.EmpId,
                            h.EmpName,
                            h.Salary,
                            p.DeptId
                        };

var groupQuery = from q in query
                         group q by q.DeptId into g
                         select new 
                         {
                             DeptCount = g.Count(),
                             Empname=g.Select(s=>s.EmpName).First(),
                             Salary = g.Select(s => s.Salary).First(),
                             EmpId = g.Select(s => s.EmpId).First()
                         };

return groupQuery.ToList();

Table 架构:

作为 sql,您应该对每个您想要查看的未聚合字段进行分组

试试这个:

var query = from h in context.Employee
                    join p in context.Dept on h.EmpId equals p.DeptId
                    select new
                    {
                        h.EmpId,
                        h.EmpName,
                        h.Salary,
                        p.DeptId
                    };

var groupQuery = from q in query
                         group q by new {EmpId= q.EmpId, EmpName = q.EmpName, Salary = q.Salary} into g
                         select new 
                         {
                             DeptCount = g.Count(),
                             Empname=g.Key.EmpName,
                             Salary = g.Key.Salary,
                             EmpId = g.Key.EmpId
                         };

return groupQuery.ToList();

你也可以这样操作

var query = from h in context.Employee
            join p in context.Dept on h.EmpId equals p.DeptId
            group new {h, p} by h into g
                     select new 
                     {
                         DeptCount = g.Count(),
                         Empname=g.Key.EmpName,
                         Salary = g.Key.Salary,
                         EmpId = g.Key.EmpId
                     };

你还应该注意没有 Dept 的 Employee 并在加入时使用 DefaultIfEmpty:

var query = from h in context.Employee
        join p in context.Dept on h.EmpId equals p.DeptId into xp
        from jp in xp.DefaultIfEmpty()
        group new {h, jp} by h into g
                 select new 
                 {
                     DeptCount = g.Count(),
                     Empname = g.Key.EmpName,
                     Salary = g.Key.Salary,
                     EmpId = g.Key.EmpId
                 };