开发者

linq expression error:cant display column?

开发者 https://www.devze.com 2023-04-09 22:03 出处:网络
i am trying to join my linq query with another table. How can I display the name in the Customer table? I am getting an error: does not contain a definition for \'Name\'?

i am trying to join my linq query with another table. How can I display the name in the Customer table? 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开发者_StackOverflow.Price)
}


You're grouping p (i.e. purchases) by date - so the customer details are no longer present.

Try this instead:

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)
}

Or alternatively:

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)
}


SalesPerYear is an IGrouping. You need to access Name, CustomerID etc off the Value property of the IGrouping.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号