我有一个清单:
List<Student> lstStudents = GetConditionalStudents();
我还有一个清单:
List<School> allSchools = dbContext.Schools.ToList();
每个学校都有学生名单
public class School
{
///other properties
public List<Student> {get;set;}
}
我被迫这样做:
List<School> schools = from p in allSchools
where p.LocationId==this.LocationId
where p.Students.Any(d=>lstStudents.Contains(d))
select p;
但它不起作用:给出错误
unable to create a constant value for .. only primitive types
编辑
我可以这样做:
List<int> integers = lstStudents.Select(s=>s.Id).ToList();
List<School> schools = from p in allSchools
where p.LocationId == this.LocationId
where p.Students.Any(d=>integers.Contains(d.Id))
select p;
但我不想使用它,因为我遇到了必须比较超过 2 个 ID 的情况,这意味着,我必须分开 2 个以上primitive datatype List
并在查询中使用它们,这是我不想要的.
如何在 linq 查询中直接使用外部列表。??
我不能使用这个:
allSchools.Where(s=>s.LocationId==this.LocationId ||
lstStudents.Contains(s)).ToList();