-1

I want to write left join using linq lambda expression. I have tried query using join but now I want to create using left join so any one can help me how can do.

Here this is my query:

var UserList = db.UserInfo
                .Join(db.Course, u => u.id, c => c.userid, (u, c) =>
                new { u, c }).GroupBy(r => r.u.id)
                .Select(g => g.OrderByDescending(r => r.c.datetime)
                .FirstOrDefault()).OrderByDescending(a => a.u.datetime).ToList();

Using this query, I don't want user data those who are not in course table, so I want to this data also in course table in userid in or not.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
coderwill
  • 804
  • 3
  • 16
  • 38
  • You haven't specified what is wrong with your query – Sergey Berezovskiy Mar 31 '17 at 12:53
  • i want to return userinfo data in coruse table user id avaible or not rieght now in course table user id is not then userinfo this user is not getting so i want this user also – coderwill Mar 31 '17 at 12:57
  • You can find your solution here http://stackoverflow.com/questions/21537469/how-to-make-left-join-in-lambda-linq-expressions – user2960398 Mar 31 '17 at 12:57
  • @user2960398 i was looking this link but i don't know how can edit my query can you please edited.. – coderwill Mar 31 '17 at 12:58
  • You can find your solution here [http://stackoverflow.com/questions/21537469/how-to-make-left-join-in-lambda-linq-expressions](http://stackoverflow.com/questions/21537469/how-to-make-left-join-in-lambda-linq-expressions) [http://stackoverflow.com/questions/30525830/linq-lambda-left-join-with-an-inner-join](http://stackoverflow.com/questions/30525830/linq-lambda-left-join-with-an-inner-join) [http://stackoverflow.com/questions/12075905/left-outer-join-in-lambda-method-syntax-in-linq](http://stackoverflow.com/questions/12075905/left-outer-join-in-lambda-method-syntax-in-linq) – user2960398 Mar 31 '17 at 12:59

1 Answers1

0

you can use

var qry = Foo.GroupJoin(
      Bar, 
      foo => foo.Foo_Id,
      bar => bar.Foo_Id,
      (x,y) => new { Foo = x, Bars = y })
.SelectMany(
      x => x.Bars.DefaultIfEmpty(),
      (x,y) => new { Foo=x.Foo, Bar=y});

ref: How do you perform a left outer join using linq extension methods

Community
  • 1
  • 1
Elsunhoty
  • 1,609
  • 14
  • 27