加入 table 和桥 table 并从主 table 获取数据,如果数据存在于桥 table 则也获取它

Join a table with bridge table and get data from main table and if data exists in bridge table then get it as well

我有两个 tables "Event(EventId,EventName,....,UpdatedBy)", "User(UserId,Username...)" 和一个桥 table "EventAttendee(EventId,UserId)"

from c in context.Events
from u in c.users 
where c.EventId == eventId
select s

以上是内部连接,所以如果 EventAttendee 中没有记录 table 那么我就得不到任何记录。

我想始终从事件 table 中获取事件详细信息,如果 EventAttendee table 中有该事件的任何记录,则获取用户 ID 和用户名。如何使用 Entity framework 在 linq to sql 中执行此操作,因为我的实体模式不显示桥 table?

只需添加DefaultIfEmpty():

from e in context.Events
from u in e.users.DefaultIfEmpty()
where e.EventId == eventId
select ...

不确定sselect中是什么,但它可能类似于

select new { e.EventName, UserId = (int?)u.UserId, u.UserName }

这将为您提供包含所有与会者的活动列表,或者 null 在没有与会者时提供与用户相关的数据。