代码之家  ›  专栏  ›  技术社区  ›  mattruma

尝试在Linq中执行子查询。..有问题!

  •  2
  • mattruma  · 技术社区  · 16 年前

    我试图将某些存储过程转换为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,
    }
    
    1 回复  |  直到 16 年前
        1
  •  5
  •   jwarzech    16 年前

    尝试:

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