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

求助LinQ语句
C# code

var q = from p in db.Products 
group p by p.CategoryID into g 
select new { g.Key, NumProducts = g.Count() };


请问这条语句怎么转换成非匿名语句

------解决方案--------------------
var q = from p in db.Products 
group p by p.CategoryID into g 
select new { g.Key, NumProducts = g.Count() };
Console.WriteLine(q.GetType().ToString());

看输出什么。
------解决方案--------------------
记住,所谓匿名,就是语法糖而已。
------解决方案--------------------
探讨
别人给出的一道题,不会。
这里要类型来干嘛呢

------解决方案--------------------
var q = from p in db.Products
group p by p.CategoryID into g
select g;
ToList();
------解决方案--------------------
啥叫非匿名,我猜是非匿名类型。

class Result
{
public int Key {set;get;} 
public int Count {set;get;}
}

var q = from p in db.Products 
group p by p.CategoryID into g 
select new Result(){ Key = g.Key, Count = g.Count() };

注意,以上代码没有经过编译验证,可能有小错误。

------解决方案--------------------
用var声明不就是匿名的~改成具体类型行吗?
------解决方案--------------------
你的select 出的是匿名,所以不能转成 非匿名,必须要用var
------解决方案--------------------
探讨
var q = from p in db.Products
group p by p.CategoryID into g
select g;
ToList();

------解决方案--------------------
其实就是一个语法糖而已了,你不用刻意的要求这样做