使用组获取linq错误

时间:2010-02-03 17:12:32

标签: linq linq-to-sql linq-group

当我添加linq分组时,我收到以下错误。

  

错误5'System.Linq.IGrouping'不包含'Description'的定义,并且没有扩展方法'Description'接受类型'System.Linq.IGrouping'的第一个参数可以找到(你是否错过了使用指令或程序集引用?)

using (var db = new DataContext())
{
     var query = from emp in db.Employees
                               .Where( e=> e.IsDeleted == false && e.DivisionId == divisionId)
                 from rev in db.Reviews
                               .Where( r=> r.EmployeeID == emp.EmployeeId && r.IsDeleted == false && r.ReviewPeriodId == reviewPeriodId)
                               .DefaultIfEmpty()
                 from obj in db.Objectives
                               .Where( o=> o.ReviewId == rev.ReviewId && o.IsDeleted == false)
                               .DefaultIfEmpty()
                 from objps in db.ObjectiveProgressStatusLanguages
                                .Where( s=> s.ObjectiveProgressStatusId == obj.ObjectiveProgressStatusId && s.LanguageId == langueageId)
                                .DefaultIfEmpty()
                 group objps by new {objps.Description, objps.StatusId into opsgroup

                 select new
                 { 
                     Status = opsgroup.Description, 
                     StatusId = opsgroup.StatusId,
                     Count = opsgroup.Count()  
                 };
    return query.CopyToDataTable();

1 个答案:

答案 0 :(得分:1)

这些字段应该是Key的一部分。尝试将其更改为:

  select new {
        Status = opsgroup.Key.Description,
        StatusId = opsgroup.Key.StatusId,
        Count = opsgroup.Count()
  }