IQueryable LINQ排序和分组

时间:2013-06-24 17:01:38

标签: sql linq list iqueryable

我有以下(不正确的)LINQ功能(更新):

public IQueryable<ClassUsageReport> GetClassUsage()
    {
        //Class Code, Title, Usage, FiscalYear
        var queryable = (from agencyplan in _agencyPlansList
                         join classSchedule2012 in _classSchedule2012List
                            on agencyplan.Id equals classSchedule2012.AgencyPlanId
                         join classes in _classesList
                            on classSchedule2012.Class.Id equals classes.Id
                         orderby agencyplan.PlanYear.FiscalYear descending, classes.ClassCode.Count() descending
                         group agencyplan by new
                         {
                             agencyplan.PlanYear.FiscalYear,
                             classes.ClassCode,
                             classes.Title 
                         } into gcs
                         select new ClassUsageReport
                         {
                             ClassCode = gcs.Key.ClassCode,
                             Title = gcs.Key.Title,
                             Usage = gcs.Key.ClassCode.Count(),
                             FiscalYear = gcs.Key.FiscalYear 
                         }
                        );

        return queryable.AsQueryable();
    }

我遇到Group By和Order By条款的问题。还有COUNT()。

我编写了正确的SQL语句,根据需要(和预期)生成结果:

select py.fiscalyear, c.classcode, c.title, count(c.classcode) as usage from classschedule2012 cs
inner join classes c on cs.class_id = c.id
inner join agencyplans ap on cs.agencyplanid = ap.Id 
inner join planyears py on ap.planyear_id = py.id
group by py.fiscalyear, c.classcode, c.title
order by py.fiscalyear desc, usage desc

我在LINQ语句中对分组和排序做错了什么? 我希望它包含像我的SQL一样的“使用”。如何才能正确反映真实计数?正如查询目前一样,它每行只返回“9”。这与我的SQL不匹配,因为真正的结果应该是“55,44,14,13”等......

编辑:2013年11月14日

以下是最终结果:

   public IQueryable<ClassUsageReport> GetClassUsage()
   {
        //Class Code, Title, Usage, FiscalYear
        var queryable = (from agencyplan in _agencyPlansList
                         join classSchedule2012 in _classSchedule2012List
                            on agencyplan.Id equals classSchedule2012.AgencyPlanId
                         join classes in _classesList
                            on classSchedule2012.Class.Id equals classes.Id
                         where classes.Active = true
                         orderby agencyplan.PlanYear.FiscalYear descending
                         group agencyplan by new
                         {
                             agencyplan.PlanYear.FiscalYear,
                             classes.ClassCode,
                             classes.Title
                         } into gcs
                         select new ClassUsageReport
                         {
                             ClassCode = gcs.Key.ClassCode,
                             Title = gcs.Key.Title,
                             Usage = gcs.Count(),
                             FiscalYear = gcs.Key.FiscalYear
                         }
                        );

        return queryable.AsQueryable().OrderByDescending(x => x.FiscalYear).ThenByDescending(x => x.Usage);
    }

2 个答案:

答案 0 :(得分:3)

分组后,您只能访问已分组的列并汇总其他列的数据(如SUM或COUNT)。在查询的选择部分中,您需要使用g.代替classes.

答案 1 :(得分:2)

尝试在select语句中使用g.Key。