简体   繁体   中英

Converting SQL to LINQ (inner join into a left outer join) C#

Hi I am trying to convert the below piece of SQL into LINQ (only learning linq so bear with me)

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

I know how to do inner joins ok with the join in on equals linq format and ive read of how to do left outer joins using the DefaultIfEmpty() property but I am unsure of how to proceed in doing an inner join immediately followed by an left outer join. Can anyone advise me on how to go from there? Any help or pointing me in the right direction to look would be appreciated thanks.

As in SQL, you can do an inner join followed by another left join

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 
    {
       //....
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM