0

I want to return ALL items from db.Accounts. How do I do a right join on the below linq query?

        var query = (
                     from tradeTbl in db.Trades
                     join acctTbl in db.Accounts on tradeTbl.AccountID equals acctTbl.AccountID

I've tried changing to

        var query = (
                    from acctTbl in db.Accounts
                    join tradeTbl in db.Trades on acctTbl.AccountID equals tradeTbl.AccountID
                    where acctTbl.AccountActive == true

still not working... if I put that same query in SSMS and change it to LEFT JOIN it works in SSMS

solarissf
  • 1,199
  • 2
  • 23
  • 58

1 Answers1

1

Just write it in terms of a left (outer) join by reversing the joined tables.

var query =
    from a in db.Accounts
    join t in db.Trades on a.AccountID equals t.AccountID into ts
    from t in ts.DefaultIfEmpty()
    select ...;
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • I must be missing something simple. I've tried yours and it still does not return all values in db.Accounts. If I put that same thing in SSMS and change it to LEFT JOIN, it works. – solarissf Sep 27 '16 at 18:25
  • Sorry I'm going all over the place. It _should_ have been a left outer join, but I was mixing it up with an inner join. – Jeff Mercado Sep 27 '16 at 18:29