如何在linq查询中使用`where`子句,同时执行`group by`?

时间:2015-06-22 08:11:41

标签: c# .net database linq

我有以下Linq声明:

var packs = from r in new XPQuery<Roll>(session)
            join p in new XPQuery<Pack>(session) on r.Pack equals p
            group new { r, p } by new { r.Pack } into gb
            select new
            {
                Length = gb.Sum(x => x.r.Length),
                ALength = gb.Where(a => a.r.Grade == "A").Sum(x => x.r.Length),
            };

gcPack.DataSource = packs;

执行时,我收到运行错误,其中包含以下内容:

  

其他信息:物业路径&#39; r&#39;类型&#39; DataRepository.Roll&#39;是不正确的因为会员&#39; DataRepository.Roll.r&#39;不存在。

我知道ALenght = gb.Where(a => a.r.Grade == "A").Sum(x => x.r.Lenght)会导致运行时错误,因为当我删除它时,代码执行时没有任何错误。

是否可以在where语句中使用select

0 个答案:

没有答案
相关问题