我有一个父母/子女关系的模型。Year和WeekYears: YearID, NameWeeks: WeekID, YearID, Name因此,如果我有YearSelected,要获得一年中的所有周数,我会:List<Week> weeks = db.weeks.Where(w => w.YearID == model.YearSelected).ToList();model.WeekList = new SelectList(weeks, "WeekID", "Name");但是在数据库中我只保存WeekID所以只有model.WeekSelected所以我如何获得同年的同一周 model.WeekSelected我可以先查询以获取YearID,但想知道是否可以在一行中完成。int YearID = db.weeks.Where(w => w.WeekID == model.WeekSelected) .SingleOrDefault().YearID.Value;
1 回答
尚方宝剑之说
TA贡献1788条经验 获得超4个赞
您将需要使用子查询来获取YearID:
List<Week> weeks = db.weeks
.Where(w => w.YearID == db.weeks.First(w => w.WeekID == model.WeekSelected).YearID)
.ToList();
当您知道WeekSelected只匹配一行时,这将生成更简单的SQL,不确定是否会对性能产生影响:
List<Week> weeks = db.weeks
.Where(w => w.YearID == db.weeks.Single(w => w.WeekID == model.WeekSelected).YearID)
.ToList();
- 1 回答
- 0 关注
- 176 浏览
添加回答
举报
0/150
提交
取消