0

有人可以帮忙在c#中将以下sql转换为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 
4

1 回答 1

2

交叉连接通常表示为查询表达式中的多个 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 中的作用。

于 2011-01-17T17:13:54.480 回答