linq 表达式错误:无法显示列?

发布于 2024-12-07 17:48:53 字数 344 浏览 0 评论 0原文

我正在尝试将 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)
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洛阳烟雨空心柳 2024-12-14 17:48:53

您按日期对 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)
}

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)
}
风筝在阴天搁浅。 2024-12-14 17:48:53

SalesPerYear 是一个 IGrouping。您需要从 IGrouping 的 Value 属性访问 Name、CustomerID 等。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文