Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将LINQ转换为不包含的实体语句转换为Lambda表达式_Lambda_Linq To Entities - Fatal编程技术网

将LINQ转换为不包含的实体语句转换为Lambda表达式

将LINQ转换为不包含的实体语句转换为Lambda表达式,lambda,linq-to-entities,Lambda,Linq To Entities,我有三个实体,如下所示 Student { StudentID, Name, Age } Parent { ParentID, Name, Age } StudentParent { StudentParentID, StudentID, ParentID } 我需要一份可以查询的学生名单,这些学生都已经到了一定的年龄,没有父母。我目前正在使用以下代码,这是可行的 IQueryable<Student> Student s = from s in db.Students

我有三个实体,如下所示

Student { StudentID, Name, Age } 

Parent { ParentID, Name, Age } 

StudentParent { StudentParentID, StudentID, ParentID }
我需要一份可以查询的学生名单,这些学生都已经到了一定的年龄,没有父母。我目前正在使用以下代码,这是可行的

IQueryable<Student> Student s = from s in db.Students
                                 where s.Age == 18
                                 where !(from sp in db.StudentParent where sp.StudentID == s.StudentID select sp.StudentID).Contains(s.StudentID)
                                 select s;
IQueryable Student s=来自db.Students中的s
其中s.Age==18
哪里(从db.StudentParent中的sp,其中sp.StudentID==s.StudentID选择sp.StudentID)。包含(s.StudentID)
选择s;
我只是想帮助您将其转换为Lambda表达式。

这应该可以:

db.Students.Where( s =>
    s.Age == 18 &&  db.StudentParent.All(sp => sp.StudentID != s.StudentID)
);

编辑:假设您没有从学生到家长的链接;如果需要,请使用它而不是联接,以提高可读性。

您应该在实体模型中创建关联

然后你就可以写了

db.Students.Where(s => s.Age == 18 && s.Parents.Count == 0)

在使用ORM时,您不应该需要显式查询联接表(例如您的
StudentParent
)。

前面有一个类似的问题:。答案比目前的答案要简洁一些。这很有效,我只做了一点小改动,再次感谢db.Students.Where(s=>s.Age==18&&s.StudentParent.All(sp=>sp.StudentID!=s.StudentID));