使用交叉联接将 SQL 转换为 Linq

发布于 2024-10-12 14:08:03 字数 396 浏览 2 评论 0原文

有人可以帮忙将以下 sql 转换为 c# 中的 linq 吗?

select s.SYSTEM_NAME,
       r.RESET_CODE,
       COUNT(v.reset_code) 
  from (select distinct system_name 
          from tbl) s 
cross join (select distinct reset_code 
              from tbl) r 
left join tbl v on v.SYSTEM_NAME = s.SYSTEM_NAME 
               and v.RESET_CODE=r.RESET_CODE 
 group by s.SYSTEM_NAME,r.RESET_CODE 

Can someone please help in converting the following sql to linq in c#?

select s.SYSTEM_NAME,
       r.RESET_CODE,
       COUNT(v.reset_code) 
  from (select distinct system_name 
          from tbl) s 
cross join (select distinct reset_code 
              from tbl) r 
left join tbl v on v.SYSTEM_NAME = s.SYSTEM_NAME 
               and v.RESET_CODE=r.RESET_CODE 
 group by s.SYSTEM_NAME,r.RESET_CODE 

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

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

发布评论

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

评论(1

ら栖息 2024-10-19 14:08:03

交叉联接通常表示为查询表达式中的多个 from 子句,或扩展方法语法中对 SelectMany 的调用。

因此,您的查询的第一部分可能是:

var query = from systemName in db.Table.Select(x => x.SystemName).Distinct()
            from resetCode in db.Table.Select(x => x.ResetCode).Distinct()
            ...

左外连接通常用“join ... into ...”查询表示,可能是这样的:

var query = from systemName in db.Table.Select(x => x.SystemName).Distinct()
            from resetCode in db.Table.Select(x => x.ResetCode).Distinct()
            join tmp in db.Table on 
                new { ResetCode = resetCode, SystemName = systemName } 
                equals new { tmp.ResetCode, tmp.SystemName }
                into tmpGroup
            select new { ResetCode = resetCode,
                         SystemName = systemName,
                         Count = tmpGroup.Count() };

老实说,我不确定 Count 部分......我不能 100% 确定 COUNT(v.ResetCode) 在原始 SQL 中的作用。

Cross joins are generally represented as multiple from clauses in a query expression, or a call to SelectMany in extension method syntax.

So the first part of your query might be:

var query = from systemName in db.Table.Select(x => x.SystemName).Distinct()
            from resetCode in db.Table.Select(x => x.ResetCode).Distinct()
            ...

Left outer joins are usually represented with a "join ... into ..." query, possibly like this:

var query = from systemName in db.Table.Select(x => x.SystemName).Distinct()
            from resetCode in db.Table.Select(x => x.ResetCode).Distinct()
            join tmp in db.Table on 
                new { ResetCode = resetCode, SystemName = systemName } 
                equals new { tmp.ResetCode, tmp.SystemName }
                into tmpGroup
            select new { ResetCode = resetCode,
                         SystemName = systemName,
                         Count = tmpGroup.Count() };

I'm not sure about the Count part, to be honest... I'm not 100% sure what the COUNT(v.ResetCode) does in your original SQL.

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