Search code examples
linqlinq-to-sqllinq-group

Getting linq error with group


I am getting the following error when I add the linq grouping.

Error 5 'System.Linq.IGrouping' does not contain a definition for 'Description' and no extension method 'Description' accepting a first argument of type 'System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?)

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

Solution

  • Those fields should be part of the Key. Try changing it to:

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