我有这段代码:
ClassroomList.Where(x => x.StudentList.Any(y => y.IsMale) == true);
此代码仅在StudentList ClassroomList
中的所有学生返回时才返回IsMale == true
,但如果至少有IsMale == false
,则返回none。
如何始终返回仅包含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
答案 0 :(得分:2)
根据编辑的预期输出,您可以使用:
var results = (from r in ClassroomList
let grp = r.StudentList.GroupBy(x => x.IsMale)
select new
{
ClassRoomsCount = ClassroomList.Count(),
StudentsCount = r.StudentList.Count,
Male = grp.Where(x => x.Key).Count(),
Female = grp.Where(x => !x.Key).Count()
}).ToList();
旁注:你可以删除这一行public ClassroomList(){}
编译器已经创建了一个隐藏的默认构造函数,所以不需要自己动手。
答案 1 :(得分:2)
你们都在那里,只需使用if((' '<=i && i<'A') || (i>'Z' && i<'a') || ('z'<i && i<=126))
扩展名。
All
更新问题后,您似乎需要这样的事情。
ClassroomList.Where(x => x.StudentList.All(y => y.IsMale));
答案 2 :(得分:1)
添加内部过滤器。
ClassroomList.Where(x => x.StudentList.All(y => y.IsMale) )
;
更新: 所以你需要这样吗?
ClassroomList.Select(x => new ClassroomList{
StudentList = x.StudentList.Where(y => y.IsMale).ToList()
}
)
;