LINQ LEFT JOIN, where the article does not work

I need to return a list of all events and any rsvps that the user may have for the event. However, regardless of the username I pass in, it returns every rsvp. My Linq request →

   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);

Relationships are Events.EventID = RSVPs.EventID

+5
source share
3 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
};
+8
source

Do it like this:

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});
0
source

If you just want to filter the RSVPs property of your events in place, then I think you could probably use something like

var events = _context.Context.Events;

foreach(var event in events)
{
    // Assuming the property is named RSVPs
    event.RSVPs = event.RSVPs.Where(o => o.UserName.Equals(userName));
}

return events;

I would not consider it neat, though.

0
source

All Articles