Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ LEFT JOIN where clause not working

Tags:

c#

linq

I need to return a list of all the events and any rsvps a user may have for an event. However, regardless of the username I pass, It returns every single rsvp. My linq Query ->

   return (from events in this._context.Context.Events
           join rsvps in (this._context.Context.RSVPs
                          .Where(o=> o.UserName == userName))
           on events equals rsvps.Event into re
           from rsvps in re.DefaultIfEmpty()
           select events);

Relationship is
Events.EventID = RSVPs.EventID

like image 514
user1106741 Avatar asked Feb 26 '13 06:02

user1106741


2 Answers

from e in _context.Context.Events
join r in _context.Context.RSVPs.Where(o => o.UserName == userName)
    on e.EventID equals r.EventID into g
select new {
    Event = e,
    Rsvps = g
};
like image 78
Sergey Berezovskiy Avatar answered Sep 21 '22 17:09

Sergey Berezovskiy


Do it this way:

return (from events in this._context.Context.Events
        join rsvps in this._context.Context.RSVPs
        on events.EventIDequals equals rsvps.EventID into re
        from c in re.DefaultIfEmpty()
        where c.UserName == userName
        select new {events,rsvps});
like image 31
Maryam Arshi Avatar answered Sep 25 '22 17:09

Maryam Arshi