linq expression error:cant display column name
I am using LinqPad 4(nutshell开发者_如何学Python database) and I am trying to display the (Customer) Name which is a column in the Customer table. How can i display the Name in this query because now I am getting an error: does not contain a definition for 'Name'?
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)
}
Try this...
I assume you have a Purchase table with (Price, CustomerID and Date) columns..
from p in Purchases
group p by p.Date.Year into SalesByYear
select new {
customerid = SalesByYear.First().CustomerID,
year=SalesByYear.Key,
TotalVal = SalesByYear.Sum(g => g.Price)
}
You've already grouped by CustomerID, therefore it is the grouping key. i.e. in your query, you should say: customerid = SalesByYear.Key
. Not sure where the year comes from in your query.
精彩评论