首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

关于LINQ to sql grou by 的有关问题

2012-03-08 
关于LINQ to sql grou by 的问题我用linq to sql 做了group by 源代码是这样的:C# codevar s from p in

关于LINQ to sql grou by 的问题
我用linq to sql 做了group by 源代码是这样的:

C# code
var s = from p in context.CarCCX_Info                    where p.Werks == strWerks && p.Zupdate == strZupdate                    group p by p.Matnr into g                    select g;


我想返回的是list<实体类> 这种类型的,但是一直报错误 
错误1无法将类型“System.Collections.Generic.List<System.Linq.IGrouping<string,YLPPENT.CarCCX_Info>>”隐式转换为“System.Collections.Generic.List<YLPPENT.CarCCX_Info>”
请问需要怎么解决啊?

[解决办法]
var s = from p in context.CarCCX_Info
where p.Werks == strWerks && p.Zupdate == strZupdate
group p by p.Matnr into g
from x in g
select x;
[解决办法]
OR:

var s = from p in context.CarCCX_Info
where p.Werks == strWerks && p.Zupdate == strZupdate
group p by p.Matnr into g
select g.First();
[解决办法]
var s = from p in context.CarCCX_Info
where p.Werks == strWerks && p.Zupdate == strZupdate
group p by new { p.Matnr, p.Maktx, p.Werks, p.Ferth, p.Normt, p.ZColor }
into g
select new { iCount = g.Count(), g.Key, Items=g.ToList() };

另外,你只取到一条数据,就说明你只有1组啊。
[解决办法]
select new { } 返回的是新类,不再是你的List<你定义的类>
[解决办法]
group by 返回的也不再是你定义的类

热点排行