将左外连接转换为与LINQ相关的2个条件

时间:2016-11-23 12:16:41

标签: c# linq tsql

我有一个TSQL代码,它有两个条件关系和左外连接,但我无法将它转换为LINQ语法。

import java.util.Scanner;
public class Test {

    public static void main(String ... args){
        Scanner scan = new Scanner(System.in);
        int size = 6;
        int[][]  m = new int[size][size];

      //numbers input
        for(int i=0; i<size; i++)
        {
          for(int j=0; j<size; j++)
           {
            m[i][j] = scan.nextInt();
           }
        } 

        int temp = 0, MaxSum = -99999;

        for (int i=0; i<size; ++i) {
            for (int j=0; j<size; ++j) {
                if (j+2 < size && i+2 < size) {
                    temp = m[i][j] + m[i][j+1] + m[i][j+2] + m[i+1][j+1] + m[i+2][j] + m[i+2][j+1] + m[i+2][j+2];
                    if (temp >= MaxSum) {
                        MaxSum = temp;
                    }
                }
            }
        }   
        System.out.println(MaxSum);

    }

}

我修改了这段代码,但它没有回复SELECT ss.StateID, Count(CASE WHEN ss.StateID = sr.FromStateID OR ss.StateID = sr.ToStateID THEN 1 END) AS CountState FROM SupStates ss LEFT JOIN SupRules sr ON ss.StateID IN ( sr.FromStateID, sr.ToStateID) GROUP BY ss.StateID

StateID

我想在结果中添加带有0关系的StateID。有没有办法用LINQ重写它?

1 个答案:

答案 0 :(得分:3)

你的Linq没有返回任何0的原因是因为它正在进行内连接。

我相信你可以用类似的东西来实现(如果代码实际上没有运行,请原谅我,我的linq有点生锈!):

from ss in this.Data()
select new {
    ss.StateID,
    CountState = (from sp in supRule 
                  where ss.StateID == sp.FromStatusID || ss.StateID == sp.ToStateID 
                  select sp.Id).Count()
}