LINQ LEFT JOIN where clause not working

前端 未结 3 1609
野趣味
野趣味 2021-01-05 18:04

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

相关标签:
3条回答
  • 2021-01-05 18:44
    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
    };
    
    0 讨论(0)
  • 2021-01-05 18:48

    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});
    
    0 讨论(0)
  • 2021-01-05 18:49

    If you just want to filter the RSVPs property of your Events in-place, then I guess you probably could do 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 wouldn't consider it that neat though.

    0 讨论(0)
提交回复
热议问题