我正在尝试将linq查询与另一个表联系起来。如何在Customer表中显示名称?我收到错误:不包含“姓名”的定义?
from p in Purchases
join c in Customers on p.CustomerID equals c.ID
group p by p.Date.Year into SalesPerYear
select new {
customername= SalesPerYear.First().Name,
customerid= SalesPerYear.First().CustomerID,
totalsales= SalesPerYear.Sum(x=>x.Price)
}
答案 0 :(得分:0)
您按日期对p
(即购买)进行分组 - 因此客户详细信息不再存在。
请改为尝试:
from p in Purchases
join c in Customers on p.CustomerID equals c.ID
group new { p, c } by p.Date.Year into SalesPerYear
select new {
CustomerName = SalesPerYear.First().c.Name,
CustomerId = SalesPerYear.First().p.CustomerID,
TotalSales = SalesPerYear.Sum(x => x.p.Price)
}
或者:
from p in Purchases
join c in Customers on p.CustomerID equals c.ID
group new { p.CustomerId, p.Price, c.CustomerName }
by p.Date.Year into SalesPerYear
select new {
SalesPerYear.First().CustomerName,
SalesPerYear.First().CustomerId
TotalSales = SalesPerYear.Sum(x => x.Price)
}
答案 1 :(得分:0)
SalesPerYear是一个IGrouping。您需要从IGrouping的Value属性访问Name,CustomerID等。