[英]With Entity Framework Core 5, how to nest a query and still have an IQueryable?
I have two entities that I want to combine and return just one IQueryable
from it.我有两个实体,我想合并并从中只返回一个IQueryable
。 That is, as an example, think of a conference app where I have Speakers
and Sessions
and Events
.也就是说,例如,考虑一个会议应用程序,其中我有Speakers
和Sessions
和Events
。
If I do a big join of all three and then I ask for all the sessions associated with one speaker (across all events), I get a return record for every event when in reality I just want one speaker record with all the sessions as detail.如果我将所有三个都进行大连接,然后我要求与一位演讲者相关的所有会话(在所有事件中),我会得到每个事件的返回记录,而实际上我只想要一个演讲者记录,其中包含所有会话的详细信息.
That is, my EF query would look like this:也就是说,我的 EF 查询看起来像这样:
var q1 = from speaker in db.speakers
join sessions in db.sessions on speaker.Id equals session.speakerId
join event in db.sessions on session.EventId equals event.Id
where speaker.Id = 101
What I want is something like the ability to first, create a speaker query我想要的是首先创建演讲者查询的能力
var q2 = from speaker in db.speakers
where speaker.Id = 101
Then, create another query that gives me all the speakers sessions over all years然后,创建另一个查询,为我提供所有年份的所有演讲者会话
var q3 = join sessions in db.sessions on speaker.Id equals session.speakerId
join event in db.sessions on session.EventId equals event.Id
where session.AttendeeId = 101
and finally, I'm really looking for one query, qFinal that combines them.最后,我真的在寻找一个查询,qFinal 结合了它们。
var qFinal = q2 that has a collection of sessions, or basically, what q3 returns.
Keeping in mind that the Attendee
entity contains a collection of sessions.请记住, Attendee
实体包含会话集合。
I realize that my pseudo code above does not really compile but my real problem is much more complex and I thought it easier to explain in simpler terms.我意识到我上面的伪代码并没有真正编译,但我的真正问题要复杂得多,我认为用更简单的术语解释起来更容易。
I think here you need Include
usage.我认为在这里您需要Include
用法。
var query = db.speakers
.Include(s => s.sessions.Where(x => x.AttendeeId = 101))
.Where(s => s.Id = 101);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.