简体   繁体   English

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

[英]LINQ - query on query results (some complex one)

Need some help in writing LINQ from the following SQL.在从以下 SQL 编写 LINQ 时需要一些帮助。 The main problem is double grouping.主要问题是双重分组。 I'm stacked in the second grouping我排在第二

group by s.[e-year], s.[e-month]

Don't know how to implement.不知道如何实施。

Thanks a lot.非常感谢。

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

For translating SQL to LINQ query comprehension:将 SQL 转换为 LINQ 查询理解:

  1. Translate FROM subselects as separately declared variables.将 FROM 子选择翻译为单独声明的变量。
  2. Translate each clause in LINQ clause order, translating monadic operators ( DISTINCT , TOP , etc) into functions applied to the whole LINQ query.按 LINQ 子句顺序翻译每个子句,将一元运算符( DISTINCTTOP等)翻译成应用于整个 LINQ 查询的函数。
  3. Use table aliases as range variables.使用表别名作为范围变量。 Use column aliases as anonymous type field names.使用列别名作为匿名类型字段名称。
  4. Use anonymous types ( new { ... } ) for multiple columns.对多列使用匿名类型( new { ... } )。
  5. Left Join is simulated by using a into join_variable and doing another from from the join variable followed by .DefaultIfEmpty() . Left Join 是通过使用 into join_variable 并从 join 变量中执行另一个来模拟的,然后是.DefaultIfEmpty()
  6. Replace COALESCE with the conditional operator and a null test.用条件运算符和空测试替换COALESCE
  7. Translate IN to .Contains() and NOT IN to !IN转换为.Contains()NOT IN! ... Contains() ... Contains()
  8. SELECT * must be replaced with select range_variable or for joins, an anonymous object containing all the range variables. SELECT *必须替换为 select range_variable 或对于连接,一个包含所有范围变量的匿名对象。
  9. SELECT fields must be replaced with select new { ... } creating an anonymous object with all the desired fields or expressions. SELECT字段必须替换为select new { ... }创建一个包含所有所需字段或表达式的匿名对象。
  10. Proper FULL OUTER JOIN must be handled with an extension method.必须使用扩展方法处理正确的FULL OUTER JOIN

Note: Your SQL query is using a SQL trick ( SELECT x ... GROUP BY x ) to perform the equivalent of a DISTINCT , which should be used as it expresses the intent more clearly.注意:您的 SQL 查询正在使用 SQL 技巧( SELECT x ... GROUP BY x )来执行DISTINCT的等效操作,应该使用它,因为它更清楚地表达了意图。

So, for your SQL query:因此,对于您的 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() };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM