有没有办法使用 NHibernate 使这个查询更容易?
为了便于理解,这是我想用 NHibernate 创建的查询:
Select * from Task
Where task_id not in
(Select task_id from UserTask
Where solved = 1 AND [user_id] = 1)
这是我在 C# 中使用 NHibernate 编写的代码
public IList<Task> RandomTasks(List<int> subject_ids, int userId)
{
//Gets all Ids where the User has solved the Task
List<int> task_ids = new List<int>(_session.Query<UserTask>()
.Where(x => x.User.user_id == userId)
.Where(x => x.solved)
.Select(x => x.Task.task_id));
//Gets all Tasks except the task_ids from the result of the query befor
var query = _session.QueryOver<Task>()
.Where(s => s.Subject.subject_id.IsIn(subject_ids))
.Where(t => !t.task_id.IsIn(task_ids))
.Take(10)
.List();
return query;
}
查询返回正确的结果,但我认为可能有更简单的方法来获得相同的结果。