我要为-
更新属性“ IsQualified = True”我尝试过的操作
var results = studentsList
.Where(x => x.University= OPQ)
.GroupBy(x => x.GroupID)
.SelectMany(g => g)
.ToList();
results.ForEach(x => x.IsQualified = true);
这不起作用。请帮助我。
我的代码-
public class StudentInfo
{
public string Student { get; set; }
public string University { get; set; }
public int GroupID { get; set; }
public bool IsQualified {get; set;}
}
List<StudentInfo> studentsList = new List<StudentInfo>();
StudentsList.Add( new StudentInfo { Student="John", University="ABC", GroupID = 1, IsQualified=False});
StudentsList.Add( new StudentInfo { Student="Jack", University="DEF", GroupID = 1, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Peter", University="GHI", GroupID = 1, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Donald", University="JKL", GroupID = 2, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Olivia", University="ABC", GroupID = 2, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Emity", University="OPQ", GroupID = 2, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Emma", University="GHI", GroupID = 2, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Alan", University="DEF", GroupID = 2, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Adam", University="GHI", GroupID = 3, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Jacob", University="ABC", GroupID = 3, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Matthew", University="OPQ", GroupID = 3, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Saint", University="VWX", GroupID = 3, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Joshua", University="TRP", GroupID = 3, IsQualified=False} );
StudentsList.Add( new StudentInfo { Student="Aubrey", University="STU", GroupID = 3, IsQualified=False} );
答案 0 :(得分:1)
您的过滤错误。
您需要先分组,然后再过滤。否则,您将按非常选择的列表分组。
var results = studentsList
.GroupBy(x => x.GroupID)
.Where(x => x.Any(y => y.University == "OPQ"))
.SelectMany(g => g)
.ToList();
results.ForEach(x => x.IsQualified = true);
答案 1 :(得分:0)
环绕所有StudentsList
列表以相应地更新IsQualified
。
var qualifiedGroups = StudentsList
.GroupBy(x => x.GroupID)
.Where(x => x.Any(y => y.University == "OPQ"))
.Select(g => g.Key).ToArray();
StudentsList.ForEach(x => x.IsQualified = Array.IndexOf(qualifiedGroups, x.GroupID) != -1);
foreach(var item in StudentsList)
Console.WriteLine("Group:" + item.GroupID + " Student:" + item.Student + " IsQualified:" + item.IsQualified);
输出
Group:1 Student:John IsQualified:False
Group:1 Student:Jack IsQualified:False
Group:1 Student:Peter IsQualified:False
Group:2 Student:Donald IsQualified:True
Group:2 Student:Olivia IsQualified:True
Group:2 Student:Emity IsQualified:True
Group:2 Student:Emma IsQualified:True
Group:2 Student:Alan IsQualified:True
Group:3 Student:Adam IsQualified:True
Group:3 Student:Jacob IsQualified:True
Group:3 Student:Matthew IsQualified:True
Group:3 Student:Saint IsQualified:True
Group:3 Student:Joshua IsQualified:True
Group:3 Student:Aubrey IsQualified:True
答案 2 :(得分:0)
解决方案中的问题是在对结果进行分组之前应用 where 条件。它将滤除GroupId 2和3中的其他学生,
您必须先将结果分组,然后再应用条件“ 那些至少有一名来自“ University = OPQ”的学生的分组”
var results = studentsList
.GroupBy(x => x.GroupID)
.Where(x => x.Any(y => y.University == "OPQ"))
.SelectMany(x => x)
.ToList();
然后,您可以进行foreach更新IsQualified字段,
results.ForEach(x => x.IsQualified = true);