Linq查询更新满足我条件的组中的所有项目

时间:2020-02-17 03:36:31

标签: c# linq

我要为-

更新属性“ IsQualified = True”
  1. 只有合格组的所有学生
  2. 合格的团体?那些至少有一名从“大学= OPQ”中通过的学生的小组(例如:小组2和小组3)
  3. 保留其他小组学生的“ IsQualified”属性不变。 (例如:第1组)

我尝试过的操作

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} );

3 个答案:

答案 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)

Demo on dotnet fiddle

  1. 您需要先进行分组,然后再根据情况进行分组
  2. 环绕所有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中的其他学生,

enter image description here

您必须先将结果分组,然后再应用条件“ 那些至少有一名来自“ University = OPQ”的学生的分组

var results = studentsList
            .GroupBy(x => x.GroupID)
            .Where(x => x.Any(y => y.University == "OPQ"))
            .SelectMany(x => x)
            .ToList();

enter image description here

然后,您可以进行foreach更新IsQualified字段,

results.ForEach(x => x.IsQualified = true);