2011-10-03 29 views
0

我想加入我的LINQ查詢與另一個表。我如何在客戶表中顯示名稱?我收到一個錯誤:不包含'Name'的定義?linq表達式錯誤:不能顯示列?

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

您按日期分組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) 
} 
0

SalesPerYear是IGrouping。您需要訪問IGrouping的Value屬性中的Name,CustomerID等。