开发者

SQL to Linq conversion with cross join

开发者 https://www.devze.com 2023-02-04 22:45 出处:网络
Can someone please help in converting the following sql to linq in c#? select 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 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消