我有这个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 name 'p' does not exist in the current context
为什么我不能在这里访问?我该怎么做才能解决这个问题?
答案 0 :(得分:5)
您可以使用Key
:
...
group p by p.Category into prices
select new
{
price = prices.Average(p => p.UnitPrice),
name = prices.Key.CategoryName
}