我创建了c#方法,根据特定条件筛选学生列表。
我在学生列表中应用初始过滤条件后首先创建filteredstudents
列表以填充它。
然后我在filteredstudentsWithUnits
filteredstudents
我试图让它更短,就像使用单个列表一样但是找不到方法。
有人可以建议,因为我会有额外的过滤条件,所以我真的不想创建多个列表。
private List<StudentDTO> _validateStudents(List<StudentDTO> students)
{
List<StudentDTO> filteredstudents = new List<StudentDTO>();
foreach (StudentDTO student in students)
{
if (student.age != null && student.status != "DEL")
filteredstudents.Add(student);
else
_log("create log");
}
List<StudentDTO> filteredstudentsWithUnits = new List<StudentDTO>();
foreach (StudentDTO student in filteredstudents)
{
bool valid = true;
foreach (SubjectTypes subjectType in student.SubjectTypes)
{
string value1 = subjectType.SubjectItem.Select(x => x.value1).First();
Guid StId = _context.Items.Where(x => x.Name == value1).FirstOrDefault();
if (StId != null)
valid = true;
else
{
valid = false;
_log("create log");
}
}
if (valid)
filteredstudentsWithUnits.Add(student);
}
return filteredstudentsWithUnits;
}
答案 0 :(得分:1)
如果我正确阅读了您的代码,您可以使用这个简短的LINQ查询:
[MC] System group container for systemgroup.com.apple.configurationprofiles path is/private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
[MC] Reading from public effective user settings.