[英]LINQ group by doesn't work
I have this LINQ query: 我有这个LINQ查询:
from p in Products
join c in Categories on p.CategoryID equals c.CategoryID
group p by p.Category into prices
select new {price = prices.Average(p => p.UnitPrice), name = p.Category.CategoryName }
The error I get is: 我得到的错误是:
The name 'p' does not exist in the current context
Why can't I access p here? 为什么我不能在这里访问? What should I do to resolve this issue?
我该怎么做才能解决这个问题?
You can use the Key
: 你可以使用
Key
:
...
group p by p.Category into prices
select new
{
price = prices.Average(p => p.UnitPrice),
name = prices.Key.CategoryName
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.