C# LINQ-查询结果的查询(一些复杂的查询)
在从以下SQL编写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.
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查询理解,请执行以下操作:
DISTINCT
、TOP
等)翻译为应用于整个LINQ查询的函数new{
…}
).DefaultIfEmpty()
,模拟左联接COALESCE
中的翻译为。包含()
和非翻译为代码>包含()
SELECT*
必须替换为SELECT range\u变量,或者对于联接,必须替换为包含所有范围变量的匿名对象SELECT
字段必须替换为SELECT new{
..}
使用所有所需字段或表达式创建匿名对象完全外部联接
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我已经简化了查询。