我尝试加入两个模型,然后从联接表中选择值。到目前为止,我所实现的是从我的理解和收集SO中不同解决方案的信息。但是在x中,我始终从第一个上下文context.EventSessionTeamModels
获取值,但从第二个上下文中获取值。但是,Select
块中的所有内容都需要从第二个context.SessionModels
中获取。也许我以错误的方式加入了他们。我需要知道需要做些什么。如果代码的任何其他部分不需要以下代码,请告诉我。
IQueryable<SessionShortModel> shortInfoSession = context.EventSessionTeamModels
.Join(context.SessionModels, x => x.session_id, y => y.session_id, (x, y) => x)
.Where(x => x.event_id == eid && x.bp_id == attendeeId && x.role_code == "ATD")
.Select(x => new SessionShortModel {
StartTime = x.start_date,
EndTime = x.end_date,
Day = DbFunctions.TruncateTime(x.start_date),
Year = x.start_date.Year,
Month = x.start_date.Month,
Name = x.session_name
});
答案 0 :(得分:1)
可能会解决您的问题。当您使用“x”时,您可以从第一个表中获取数据。将变量更改为“Y”您将从第二个表中获取数据。
var data =(from ep in context.EventSessionTeamModels
join e in context.SessionModels on ep.session_id equals e.session_id
where ep.event_id == eid && ep.bp_id == attendeeId && ep.role_code == "ATD"
select new SessionShortModel{
StartTime = e.start_date,
EndTime = e.end_date,
Day = DbFunctions.TruncateTime(e.start_date),
Year = e.start_date.Year,
Month = e.start_date.Month,
Name = e.session_name
});
答案 1 :(得分:0)
在EF中你必须要关系实体,EventSessionTeamModels需要有自己的SessionModels
List<SessionShortModel> shortInfoSession = context.EventSessionTeamModels
.Include("SessionModels")
.Where(x => x.SessionModels.event_id == eid && x.SessionModels.bp_id == attendeeId && x.SessionModels.role_code == "ATD")
.Select(x => new SessionShortModel {
StartTime = x.start_date,
EndTime = x.end_date,
Day = DbFunctions.TruncateTime(x.start_date),
Year = x.start_date.Year,
Month = x.start_date.Month,
Name = x.session_name
}).ToList();