实体框架 - 如何获取过滤的主记录和过滤的详细记录?
我的班级关系:ClassMaster 1----* ClassSessions
我的目标:返回所有具有 StartDateTime 大于当前日期的 ClassSession 的 ClassMaster,以及这些 ClassSession(按日期过滤)。
在 T-SQL 中,我会这样做:
select *
from ClassSession
join ClassMaster on ClassMaster.ClassId = ClassSession.ClassId
Where ClassSession.StartDateTime > getdate()
我需要在 Entity Framework 4 查询中实现相同的结果。这就是我认为它会起作用的,但没有:
var classes = ctx.ClassSessions
.Include("ClassMasters")
.Where(s => s.StartDateTime > DateTime.Now)
.Select(s => s.ClassMaster)
.ToList();
这给了我所有具有 StartDateTime > 的 ClassSession 的 ClassMaster。当前日期,但 ClassSessions 属性填充了所有 ClassSession,包括早于当前日期的。我只想要 StartDateTime > 的 ClassSessions当前日期。
我做错了什么?
非常感谢!
My class relationship: ClassMaster 1----* ClassSessions
My Goal: Return all ClassMasters that have ClassSessions with StartDateTime greater than the current date, along with these ClassSessions (filtered by date).
In T-SQL I'd do this:
select *
from ClassSession
join ClassMaster on ClassMaster.ClassId = ClassSession.ClassId
Where ClassSession.StartDateTime > getdate()
I need to realize the same results in an Entity Framework 4 query. This is what I thought it would work, but does not:
var classes = ctx.ClassSessions
.Include("ClassMasters")
.Where(s => s.StartDateTime > DateTime.Now)
.Select(s => s.ClassMaster)
.ToList();
This give's me all the ClassMasters that have some ClassSession with StartDateTime > current date, but the ClassSessions property comes filled with all ClassSessions, including older than current date. I only want ClassSessions with StartDateTime > current date.
What am I doing wrong?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在上下文中的某个位置加载了所有 ClassSession 对象,您仍然可以执行以下操作:
In case you loaded all ClassSession objects somewhere in context, you can still do this: