5

我需要返回所有事件的列表以及用户可能对某个事件拥有的任何 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

4

3 回答 3

15
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
};
于 2013-02-26T07:00:59.837 回答
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});
于 2013-02-26T07:11:45.793 回答
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;

不过,我不会认为它那么整洁。

于 2013-02-26T07:12:22.923 回答