如何在 LINQ 中包含基于条件的列表
How to include list based on a condition in LINQ
我有这个代码:
ClassroomList.Where(x => x.StudentList.Any(y => y.IsMale) == true);
此代码 returns ClassroomList
仅当 StudentList 中的所有学生 IsMale == true
但 returns none 如果至少有 1 IsMale == false
.
我如何总是 return ClassroomList 只包含 IsMale == true
.
的学生
这里是 ClassroomList 的 Class:
public partial class ClassroomList
{
public ClassroomList(){}
public list<Student> StudentList {get; set;}
}
这是学生 Class:
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
根据编辑后的预期输出,您可以使用:
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(){}
编译器已经创建了一个隐藏的默认构造函数,所以你不需要自己做。
你们都在那里,只需使用 All
扩展名。
ClassroomList.Where(x => x.StudentList.All(y => y.IsMale));
更新问题后,您似乎需要这样的东西。
var result = ClassroomList
.Select(x=> new
{
StudentListCount = x.StudentList.Count(),
MaleCount = x.StudentList.Count(c=>c.IsMale),
FemaleCount = x.StudentList.Count(c=>!c.IsMale),
};
添加内部 where 过滤器。
ClassroomList.Where(x => x.StudentList.All(y => y.IsMale) )
;
更新:
所以你需要这样吗?
ClassroomList.Select(x => new ClassroomList{
StudentList = x.StudentList.Where(y => y.IsMale).ToList()
}
)
;
我有这个代码:
ClassroomList.Where(x => x.StudentList.Any(y => y.IsMale) == true);
此代码 returns ClassroomList
仅当 StudentList 中的所有学生 IsMale == true
但 returns none 如果至少有 1 IsMale == false
.
我如何总是 return ClassroomList 只包含 IsMale == true
.
这里是 ClassroomList 的 Class:
public partial class ClassroomList
{
public ClassroomList(){}
public list<Student> StudentList {get; set;}
}
这是学生 Class:
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
根据编辑后的预期输出,您可以使用:
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(){}
编译器已经创建了一个隐藏的默认构造函数,所以你不需要自己做。
你们都在那里,只需使用 All
扩展名。
ClassroomList.Where(x => x.StudentList.All(y => y.IsMale));
更新问题后,您似乎需要这样的东西。
var result = ClassroomList
.Select(x=> new
{
StudentListCount = x.StudentList.Count(),
MaleCount = x.StudentList.Count(c=>c.IsMale),
FemaleCount = x.StudentList.Count(c=>!c.IsMale),
};
添加内部 where 过滤器。
ClassroomList.Where(x => x.StudentList.All(y => y.IsMale) )
;
更新: 所以你需要这样吗?
ClassroomList.Select(x => new ClassroomList{
StudentList = x.StudentList.Where(y => y.IsMale).ToList()
}
)
;