C# LINQ-查询结果的查询(一些复杂的查询)

C# LINQ-查询结果的查询(一些复杂的查询),c#,sql,.net,linq,subquery,C#,Sql,.net,Linq,Subquery,在从以下SQL编写LINQ时需要一些帮助。 主要问题是双重分组。 我被排在第二组 group by s.[e-year], s.[e-month] 不知道如何实施 非常感谢 select s.[e-year], s.[e-month], count(s.projectid) 'projects entranced', --------------------------------------------- (select count(subquery.

在从以下SQL编写LINQ时需要一些帮助。 主要问题是双重分组。 我被排在第二组

group by s.[e-year], s.[e-month]
不知道如何实施

非常感谢

select s.[e-year], s.[e-month], count(s.projectid) 'projects entranced',
          ---------------------------------------------
          (select count(subquery.CustomerTypeID) from
              (select count(ap.ProjectID) as 'count', c.CustomerTypeID FROM Logging_ProjectsEntrances] pe
              inner join users u on pe.userid = u.userid
              inner join Companies c on u.CompanyId = c.CompanyID
              inner join AssignedProjects up on pe.ProjectID = up.ProjectID
              inner join Projects p on up.ProjectID = p.ProjectID
              where ap.ProductID = 1 and year(pe.EntranceDate) = s.[e-year] and MONTH(pe.entrancedate) = s.[e-month] and c.CustomerTypeID = 2
              group by ap.ProjectID, c.CustomerTypeID) subquery
          group by subquery.CustomerTypeID
          )
          --------------------------------------------
  from
  (
      select YEAR(pe.EntranceDate) as 'e-year', MONTH(pe.EntranceDate) as 'e-month', up.ProjectID as 'projectid' 
      FROM Logging_ProjectsEntrances pe
      inner join AssignedProjects ap on pe.ProjectID = ap.ProjectID
      inner join Projects p on ap.ProjectID = p.ProjectID
      where ap.ProductID = 1
      group by year(pe.EntranceDate), month(pe.EntranceDate), ap.ProjectID
  ) as s
  group by s.[e-year], s.[e-month]
  order by s.[e-year] desc , s.[e-month] desc

要将SQL转换为LINQ查询理解,请执行以下操作:

  • 将Subselect转换为单独声明的变量
  • 按LINQ子句顺序翻译每个子句,将一元运算符(
    DISTINCT
    TOP
    等)翻译为应用于整个LINQ查询的函数
  • 使用表别名作为范围变量。使用列别名作为匿名类型字段名
  • 对多个列使用匿名类型(
    new{
    }
  • 通过使用into Join_变量并从Join变量执行另一个操作,然后执行
    .DefaultIfEmpty()
    ,模拟左联接
  • 用条件运算符和空测试替换
    COALESCE
  • 中的
    翻译为
    。包含()
    翻译为
    包含()
  • SELECT*
    必须替换为SELECT range\u变量,或者对于联接,必须替换为包含所有范围变量的匿名对象
  • SELECT
    字段必须替换为
    SELECT new{
    ..
    }
    使用所有所需字段或表达式创建匿名对象
  • 必须使用扩展方法处理正确的
    完全外部联接
  • 注意:您的SQL查询正在使用SQL技巧(
    SELECT x
    groupby x
    )来执行与
    不同的
    等价的操作,因为它更清楚地表达了意图

    因此,对于SQL查询:

    var subq = (from pe in projectsEntrances
                join ap in assignedProjects on pe.ProjectID equals ap.ProjectID
                join p in projects on ap.ProjectID equals p.ProjectID
                where ap.ProductID == 1
                select new { e_year = pe.EntranceDate.Year, e_month = pe.EntranceDate.Month, ap.ProjectID }).Distinct();
    
    var ans = from s in subq
              group s by new { s.e_year, s.e_month } into sg
              orderby sg.Key.e_year descending, sg.Key.e_month descending
              select new { sg.Key.e_year, sg.Key.e_month, ProjectsEntranced = sg.Count() };
    

    那你需要什么帮助呢?你被困在哪里了?我已经添加了一些细节。那么到目前为止你有什么?我已经添加了一些额外的细节。@nvoigt我已经简化了查询。