如何将下面的sql group by语句转换为Linq

发布于 2024-10-30 21:41:22 字数 2467 浏览 1 评论 0原文

我是 LINQ 新手,正在尝试将下面的 TSQL 转换为 LINQ,有什么方法可以做到这一点吗?感谢您的所有帮助。

这是我试图转换的 SQL 语句

SELECT [t5].[StatusID], [t5].[FriendlyName], sum(1) AS [Count]  
FROM (  
SELECT distinct [t0].DealID, [t0].[StatusID], [t1].[FriendlyName]  
FROM [DR_Deal] AS [t0]  
INNER JOIN [DR_DealStage] AS [t1] ON [t0].[StatusID] = [t1].[DealStageID]  
INNER JOIN [DR_Deal_ApproverInfo] AS [t2] ON [t0].[DealID] = [t2].[DealID]  
INNER JOIN [DR_Approver] AS [t3] ON [t2].[ApproverID] = ([t3].[ApproverID])  
INNER JOIN [DR_Profile] AS [t4] ON [t3].[ProfileID] = ([t4].[ProfileID])  
WHERE (LOWER([t4].[Email]) = @p0) OR (LOWER([t0].[CreatedBy]) = @p1)  
) AS [t5]  
GROUP BY [StatusID], [FriendlyName]  
ORDER BY [t5].[StatusID]

EDITS

是的,上面的sql是使用linqpad生成的,这就是我真正想要转换为linq的 <代码> SELECT COUNT(DISTINCT [t0].DealID) AS 计数,[t0].[StatusID], [t1].[FriendlyName]
来自 [DR_Deal] AS [t0]
INNER JOIN [DR_DealStage] AS [t1] ON [t0].[StatusID] = [t1].[DealStageID]
内部联接 [DR_Deal_ApproverInfo] AS [t2] ON [t0].[DealID] = [t2].[DealID]
INNER JOIN [DR_Approver] AS [t3] ON [t2].[ApproverID] = ([t3].[ApproverID])
INNER JOIN [DR_Profile] AS [t4] ON [t3].[ProfileID] = ([t4].[ProfileID])
WHERE (LOWER([t4].[Email]) = LOWER(@Email)) OR (LOWER([t0].[CreatedBy]) = LOWER(@UserName))
GROUP BY [t0].[StatusID], [t1].[FriendlyName]
排序依据 [t0].[StatusID]

我想我找到了使用 linq 执行 count(distinct) 的解决方案,这是我最初偶然发现的,但转换后的解决方案仍然不起作用,我正在考虑下面的 linq 代码不起作用,因为我正在使用 sql 2000 并且生成的 sql 具有嵌套选择,并且 sql 2000 似乎存在一些限制。我仍然不确定我的结论,可能是 linq blelow 完全错误或者可能有更好的方法来编写上面的sql。

var query = (from d in DR_Deal  
join s in DR_DealStage  
on d.StatusID equals s.DealStageID  
join da in DR_Deal_ApproverInfo  
on d.DealID equals da.DealID  
join a in DR_Approver  
on da.ApproverID equals a.ApproverID  
join p in DR_Profile  
on a.ProfileID equals p.ProfileID  
where p.Email.ToLower().Equals("[email protected]")   
|| d.CreatedBy.ToLower().Equals("test")  
group d.DealID by new { d.StatusID, s.FriendlyName}  
into grp  
select new  
{  
  StatusID = grp.Key.StatusID,  
  FriendlyName = grp.Key.FriendlyName,  
  Count = grp.Distinct().Count()  
}).OrderBy(x=> x.StatusID);

I am new to LINQ and was trying to convert the below TSQL to LINQ, Is there any way i can do this? Thanks for all the help.

This is SQL Statement I am trying to convert

SELECT [t5].[StatusID], [t5].[FriendlyName], sum(1) AS [Count]  
FROM (  
SELECT distinct [t0].DealID, [t0].[StatusID], [t1].[FriendlyName]  
FROM [DR_Deal] AS [t0]  
INNER JOIN [DR_DealStage] AS [t1] ON [t0].[StatusID] = [t1].[DealStageID]  
INNER JOIN [DR_Deal_ApproverInfo] AS [t2] ON [t0].[DealID] = [t2].[DealID]  
INNER JOIN [DR_Approver] AS [t3] ON [t2].[ApproverID] = ([t3].[ApproverID])  
INNER JOIN [DR_Profile] AS [t4] ON [t3].[ProfileID] = ([t4].[ProfileID])  
WHERE (LOWER([t4].[Email]) = @p0) OR (LOWER([t0].[CreatedBy]) = @p1)  
) AS [t5]  
GROUP BY [StatusID], [FriendlyName]  
ORDER BY [t5].[StatusID]

EDITS

yes the above sql is generated using linqpad, this is what i really want to convert to linq

SELECT COUNT(DISTINCT [t0].DealID) AS count, [t0].[StatusID], [t1].[FriendlyName]
FROM [DR_Deal] AS [t0]
INNER JOIN [DR_DealStage] AS [t1] ON [t0].[StatusID] = [t1].[DealStageID]
INNER JOIN [DR_Deal_ApproverInfo] AS [t2] ON [t0].[DealID] = [t2].[DealID]
INNER JOIN [DR_Approver] AS [t3] ON [t2].[ApproverID] = ([t3].[ApproverID])
INNER JOIN [DR_Profile] AS [t4] ON [t3].[ProfileID] = ([t4].[ProfileID])
WHERE (LOWER([t4].[Email]) = LOWER(@Email)) OR (LOWER([t0].[CreatedBy]) = LOWER(@UserName))
GROUP BY [t0].[StatusID], [t1].[FriendlyName]
ORDER BY [t0].[StatusID]

And I think I found the solution to do the count(distinct) using linq which I initially stumbled upon, but still the converted solution didn't work, I am thinking the below linq code is not working because i am using sql 2000 and the generated sql have nested selects and there seems some limitation with sql 2000. I am still not sure about my conclusions, may be the linq blelow is wrong altogther or there might be a better way to write the above sql.

var query = (from d in DR_Deal  
join s in DR_DealStage  
on d.StatusID equals s.DealStageID  
join da in DR_Deal_ApproverInfo  
on d.DealID equals da.DealID  
join a in DR_Approver  
on da.ApproverID equals a.ApproverID  
join p in DR_Profile  
on a.ProfileID equals p.ProfileID  
where p.Email.ToLower().Equals("[email protected]")   
|| d.CreatedBy.ToLower().Equals("test")  
group d.DealID by new { d.StatusID, s.FriendlyName}  
into grp  
select new  
{  
  StatusID = grp.Key.StatusID,  
  FriendlyName = grp.Key.FriendlyName,  
  Count = grp.Distinct().Count()  
}).OrderBy(x=> x.StatusID);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

待天淡蓝洁白时 2024-11-06 21:41:22

我认为有很多方法可以做到这一点,但我做了一些类似的事情并将其映射到一个对象。此声明并不完全适合您的问题,但您可以使用以下内容将不同值与聚合(具有、组、计数、总和)值等混合。

             IEnumerable<MyObject> myObj                  
              =  (from d in dbContext.DR_Deal          
             /*DO ALL YOUR JOINS */ 
             join ds in dbContext.DR_DealStage on d.statusID equals ds.dealstageID
             where ds.mycolumn == myvalue

             select new MyObject()
             {
                   groupedByObject = d,
                   singleColumnsValue = ds.columnName
                   /* For calculated values have inner LINQ statements which join
                   back to elements from the main query. These can also 
                   return objects*/
                   agregateValue = (from x in dbContext.OtherTable
                                    where x.id == d.id).Count()
             })

I think there are a number of ways to do this but I did some thing similar and mapped it to an object. This statement isn't totally right for your problem but you can use the below to mix distinct values with agregate (having, group, count, sum) values etc.

             IEnumerable<MyObject> myObj                  
              =  (from d in dbContext.DR_Deal          
             /*DO ALL YOUR JOINS */ 
             join ds in dbContext.DR_DealStage on d.statusID equals ds.dealstageID
             where ds.mycolumn == myvalue

             select new MyObject()
             {
                   groupedByObject = d,
                   singleColumnsValue = ds.columnName
                   /* For calculated values have inner LINQ statements which join
                   back to elements from the main query. These can also 
                   return objects*/
                   agregateValue = (from x in dbContext.OtherTable
                                    where x.id == d.id).Count()
             })
黑凤梨 2024-11-06 21:41:22

我总是依赖 101 个 LinQ 示例

http://msdn.microsoft.com/en-us /vcsharp/aa336746

I ALWAYS fall back on 101 LinQ samples

http://msdn.microsoft.com/en-us/vcsharp/aa336746

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文