【发布时间】:2016-07-13 05:08:27
【问题描述】:
我有这个代码:
ClassroomList.Where(x => x.StudentList.Any(y => y.IsMale) == true);
仅当 StudentList 中的所有学生 IsMale == true 时,此代码才返回 ClassroomList,但如果至少有 1 个 IsMale == false,则返回无。
如何始终返回仅包含 IsMale == true 的学生的 ClassroomList。
这是 ClassroomList 的类:
public partial class ClassroomList
{
public ClassroomList(){}
public list<Student> StudentList {get; set;}
}
这里是学生班级:
public partial class Student
{
public Student (){}
public bool IsMale {get; set;}
}
这是预期输出的示例
Classroomlist Count | Studentlist Count | Male | Female |
2 5 3 2
2 10 10 0
2 8 0 8
预期输出:
1. ClassroomList[1].Studentlist[2]
2. ClassroomList[1].Studentlist[9]
3. ClassroomList[1].Studentlist == null
【问题讨论】:
-
@Desperado 嗨谢谢我已经检查过了,它抛出一个错误,说不能将 IEnumerable 转换为 Icollection。是否不再需要使用 Any() 或 All() ?
-
哦,主要错误是“实体或复杂类型..无法在 LINQ to Entities 查询中构造”
-
StudentList = x.StudentList.Where(y => y.IsMale).ToList()