将SQL转换为LINQ(内连接到左外连接)C#

时间:2016-05-03 09:34:15

标签: c# linq left-join inner-join

您好我正在尝试将下面的SQL转换为LINQ(只学习linq所以请耐心等待)

SELECT SUM(Bt.Stake)AS TotalStake, SUM(Bt.Payout) AS TotalPayout, SUM(Bt.Stake - Bt.BetPayout) AS TotalProfitLoss, COUNT(Bt.BtID) AS NumberBts,
                      EV_MarketMix.Description
            FROM         Bt INNER JOIN
                                  Slip ON Bt.SlipID = Slip.SlipId LEFT OUTER JOIN
                                  EV_MarketMix ON Bt.MarketMixID = EV_MarketMix.MarketMixID
            WHERE(Slip.DateScanned >= @StartDate) AND(Slip.DateScanned < @EndDate)
            GROUP BY EV_MarketMix.Description

我知道如何使用join in on equals linq格式进行内连接,并且我已经阅读了如何使用DefaultIfEmpty()属性进行左外连接,但我不确定如何继续进行内部连接紧接着加入左外连接。谁能告诉我如何去那里?任何帮助或指示我正确的方向看起来将不胜感激。

1 个答案:

答案 0 :(得分:0)

与在SQL中一样,您可以执行内部联接,然后执行另一个左联接

var query = (from t1 in Context.Table1
    join t2 in Context.Table2 on //...  //inner join
    join t3 in Context.Table3 on /*...*/ into joined //left join
    from joined.DefaultIfEmpty()
    select new 
    {
       //....
    };