LINQ LEFT JOIN where子句不起作用

时间:2013-02-26 06:27:24

标签: c# linq

我需要返回所有事件的列表以及用户可能拥有的任何事件的rsvps。但是,无论我传递的用户名是什么,它都会返回每个rsvp。我的linq查询 - >

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

关系是     Events.EventID = RSVPs.EventID

3 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:1)

这样做:

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

答案 2 :(得分:0)

如果你只想过滤你的事件的RSVPs属性,那么我猜你可能会使用像

这样的东西
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;

我不会认为这很整洁。