我正在使用ASP.NET Core和Entity Framework在Visual Studio上进行开发。
这是我的查询
var Male = from s in _context.ApprovalKits
join r in _context.Registrations on s.StundetId equals r.StundetId into a
where s.RoomType.Equals(RoomType.s)
&& s.HealthCondition.Equals(HealthCondition.none)
&& r.gender.Equals(Gender.m)
select a;
r.gender
上有错误:
名称“ r”在当前上下文中不存在
如何解决我的查询?
答案 0 :(得分:1)
如果您的EF实体正确设置了关联,则您无需在Linq中使用手动加入,因为您可以轻松地做到这一点:
List<Registration> maleRegistrations = db.Registrations
.Include( r => r.ApprovalKit )
.Where( r => r.ApprovalKit.RoomType == RoomType.S )
.Where( r => r.HealthCondition == HealthCondition.None )
.Where( r => r.Gender == Gender.Male );
.ToList();
(您也可以像这样合并Where
:)
List<Registration> maleRegistrations = db.Registrations
.Include( r => r.ApprovalKit )
.Where( r =>
r.ApprovalKit.RoomType == RoomType.S &&
r.HealthCondition == HealthCondition.None &&
r.Gender == Gender.Male
)
.ToList();