C# 在一个查询中根据Linq中的外部列表筛选给定列表
我有一个清单:C# 在一个查询中根据Linq中的外部列表筛选给定列表,c#,linq,entity-framework,ef-code-first,sql-server-ce,C#,Linq,Entity Framework,Ef Code First,Sql Server Ce,我有一个清单: List<Student> lstStudents = GetConditionalStudents(); 编辑 我可以通过这样做来实现: List<int> integers = lstStudents.Select(s=>s.Id).ToList(); List<School> schools = from p in allSchools where p.LocationId == th
List<Student> lstStudents = GetConditionalStudents();
编辑
我可以通过这样做来实现:
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;
请帮忙。。。
我经历了……但他们对我没有帮助。试试:
var schools = from p in allSchools
where (p.LocationId==this.LocationId && p.Students.Any(s => lstStudents.Contains(s)))
select p;
尝试:
问题是实体框架无法将学生列表转换为有效的SQL语句-显然,数据库服务器对您
Student
类一无所知,因此实体框架无法将LINQ查询转换为数据库服务器可以理解的内容。有三种解决方案
GetConditionalStudents()
,如果可能的话,直接将生成此列表的查询包含在主查询中ToList()
,将两个列表-lststuldren
和allSchools
提取到内存中,并使用LINQ to对象处理这两个列表
在我看来,您似乎已经在执行选项二,但显然不是因为代码失败,而是我无法确定确切的缺陷。问题是实体框架无法将学生列表转换为有效的SQL语句-显然数据库服务器对您一无所知
Student
类和因此,实体框架无法将LINQ查询转换为数据库服务器可以理解的内容。有三种解决方案
GetConditionalStudents()
,如果可能的话,直接将生成此列表的查询包含在主查询中ToList()
,将两个列表-lststuldren
和allSchools
提取到内存中,并使用LINQ to对象处理这两个列表
在我看来,您似乎已经在执行选项二,但显然不是因为代码失败,而是我无法确定确切的缺陷。@ChrisSincliar..lststustudents会被正确的学生列表填充。它有正确的记录是的对不起,我刚注意到,对不起!这是使用Linq2Entities吗?:)实体框架代码首先,所有学校与学生有什么关系?他们有没有idshare@ChrisSincliar学生们会得到适当的学生名单。它有正确的记录是的对不起,我刚注意到,对不起!这是在使用Linq2Entities吗?:)Entity Framework Code First所有与LST学生相关的学校都有何身份证他们是否共享它给了我这个错误:“Internal.NET Framework Data Provider error 1025”。@ManishMishra编辑了我的答案。它给了我这个错误:“Internal.NET Framework Data Provider error 1025”。@ManishMishra编辑了我的答案。@Daniel,GetConditionalStudents()的根本目的是简化问题结构。问题在于外部列表的使用。虽然最初我只是从一个查询开始。选项3正在工作,但只有当我在一个单独的列表中“选择”查询外部的所有ID,然后进行比较时,选项3才起作用。@Daniel,GetConditionalStudents()的目的是简化问题结构。问题在于外部列表的使用。虽然最初我只是从一个查询开始。选项3正在工作,但仅当我在一个单独的列表中“选择”查询外部的所有ID,然后进行比较(但不在内部)时才起作用。
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;
allSchools.Where(s=>s.LocationId==this.LocationId ||
lstStudents.Contains(s)).ToList();
var schools = from p in allSchools
where (p.LocationId==this.LocationId && p.Students.Any(s => lstStudents.Contains(s)))
select p;