【问题标题】:Csharp linq selection with join in nested classes在嵌套类中加入 Csharp linq 选择
【发布时间】:2016-10-15 15:33:22
【问题描述】:

我有一个名为 school 的 C#class,它有一个班级列表(在学校),其中有一个教师列表,其中有一个学生列表,如下所示。我需要得到的是每个包含的老师的所有学生姓名的逗号分隔列表。我怎样才能用 linq 做到这一点? 完整代码如下:

class LinqTests
{
    static void Main(string[] args)
    {
        Debug.WriteLine("*********************************************************************");

        School rw = new School();
        for (int j = 0; j < 2; j++)
        {
            ClassInSchool sr = new ClassInSchool();
            for (int i = 0; i < 2; i++)
            {
                sr.teachers.Add(new Teacher((i % 2), "" + i));
            }
            rw.classes.Add(sr);
        }
        var elems = rw.classes.Select(sr => sr.teachers)
            .Where(l2s => l2s != null)
            .Where(l2s => l2s.Any(l2 => l2.include == true));

        Debug.WriteLine(JsonConvert.SerializeObject(elems, Formatting.None));

        Debug.WriteLine("*********************************************************************");
    }


    class School
    {
        public List<ClassInSchool> classes;
        public School()
        {
            classes = new List<ClassInSchool>();
        }
    }

    class ClassInSchool
    {
        public List<Teacher> teachers;

        public ClassInSchool()
        {
            teachers = new List<Teacher>();
        }
    }

    class Teacher
    {
        public bool include;
        public List<string> students;
        public Teacher(int includeIn, string student)
        {
            include = Convert.ToBoolean(includeIn);
            students = new List<string>();
            for (int i = 0; i < 3; i++)
            {
                students.Add(student + i);
            }
        }
    }
}

【问题讨论】:

    标签: c# linq lambda


    【解决方案1】:
        var teachers = rw.classes.Where(x => x.teachers != null)
                         .SelectMany(x => x.teachers.Where(teacher => teacher.students != null && teacher.include));
        var allStudentsNames = teachers.SelectMany(x => x.students);
        var uniqueStudentsNames = allStudentsNames.Distinct();
        var uniqueStudentsNamesCommSeparatedList = string.Join(", ", uniqueStudentsNames);
    

    您当然可以链接这些方法。

    【讨论】:

    • 感谢您的细分...它帮助我更好地理解返回值..
    【解决方案2】:

    如果你想要学生,我想你想要以下

    var students = rw.classes
        .SelectMany(c => c.teachers)
        .Where(t => t.include)
        .SelectMany(t => t.students);
    

    然后得到一个逗号分隔的字符串

    var csv = string.Join(", ", students);
    

    我删除了teachers 列表上的null 检查,因为您在ClassInSchool 构造函数中对其进行了初始化。但是,如果您需要防范可能的 null 集合,您可以这样做。

    var students = rw.classes
        .Where(c => c.teachers != null)
        .SelectMany(c => c.teachers)
        .Where(t => t.include && t.students != null)
        .SelectMany(t => t.students);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多