【问题标题】:Trying to do a sub query in Linq ... having issues!尝试在 Linq 中进行子查询...遇到问题!
【发布时间】:2023-03-03 01:23:01
【问题描述】:

我正在尝试将我的一些存储过程转换为 Linq,但遇到以下 Transact-Sql 语句的问题:

Select 
    Year(p.StartDate) As Year,
    (Select Sum(t.Units) From Time t Where Year(t.TransactionDate) = Year(p.StartDate)) As Hours,
    (Select Sum(i.Price) From Invoice i Where Year(i.CreatedDate) = Year(p.StartDate)) As Invoices
From 
    Period p
Group By
    Year(p.StartDate)
Order By
    Year(p.StartDate)

我正在与 LinqPad 合作,试图解决这个问题......任何帮助将不胜感激!

进展

到目前为止,我有以下内容......只是想弄清楚如何转换子查询:

from p in Periods
group p by p.StartDate.Year into g
orderby g.Key
select new 
{
    g.Key,
}

【问题讨论】:

  • 您能解释一下您遇到了什么样的问题/错误吗?
  • 我正在尝试转换这个 Linq ......这就是我正在努力的地方......主要是子查询。

标签: database linq linq-to-sql tsql


【解决方案1】:

试试:

from p in Periods
group p by p.StartDate.Year into g
orderby g.Key
select new
{
g.Key,
Hours = (from t in Times where t.TransactionDate.Year == g.Key select t).Sum(t => t.Units),
Invoices = (from i in Invoices where i.CreatedDate.Year == g.Key select i).Sum(i => i.Price)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-15
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    相关资源
    最近更新 更多