日期:2014-05-20  浏览次数:20755 次

关于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 返回的也不再是你定义的类