Linq Group and Count in IEnumerable
我的控制器中有下面的LINQ查询,它查询我的IEnumerable集合,当我在视图中输出结果时分组工作,但是当我尝试在列上添加计数时,分组失败得很惨。我想知道是否有人能帮忙,我已经看过前面的例子,但我遗漏了一些明显的东西。
分组//工作正常并返回分组描述
1 2 3 4 5 6 | itemdetails = (from c in db.CLIENTDETAILS join l in db.LOCATIONS on c.CLIENTNUMBER equals l.CLIENTNUMBER where c.CLIENTNUMBER == clientNumber join i in db.ITEMDETAILS on l.LOCNUMBER equals i.LOCNUMBER where i.LOCNUMBER == l.LOCNUMBER select i).GroupBy(it => it.DESC).Select(grp => grp.FirstOrDefault()).OrderBy(x => x.DESC) |
我尝试在linq/中获取group和count的内容//不工作返回错误
1 2 3 4 5 6 | itemdetails = (from c in db.CLIENTDETAILS join l in db.LOCATIONS on c.CLIENTNUMBER equals l.CLIENTNUMBER where c.CLIENTNUMBER == clientNumber join i in db.ITEMDETAILS on l.LOCNUMBER equals i.LOCNUMBER where i.LOCNUMBER == l.LOCNUMBER select i).GroupBy(it => it.DESC).Select(grp => new {DESC = grp.key, Count = grp.COUNT()}).OrderBy(x => x.DESC) |
这给了我以下错误:无法将类型System Linq IOrderedQueryable隐式转换为System.Collections.Generic.IEnumerable
一如既往地感谢你的帮助。
您的两个查询返回不同的数据,第一个查询返回
如果需要匿名类型的IEnumerable,则需要使用
1 2 3 4 5 6 | var itemdetails = (from c in db.CLIENTDETAILS join l in db.LOCATIONS on c.CLIENTNUMBER equals l.CLIENTNUMBER where c.CLIENTNUMBER == clientNumber join i in db.ITEMDETAILS on l.LOCNUMBER equals i.LOCNUMBER where i.LOCNUMBER == l.LOCNUMBER select i).GroupBy(it => it.DESC).Select(grp => new {DESC = grp.key, Count = grp.COUNT()}).OrderBy(x => x.DESC) |