CSharp - Use SelectMany to get list of new anonymous classes

Introduction

The following code uses SelectMany operator on the array of Student elements.

For each Student element in the array, SelectMany selector method will return zero or more elements of the anonymous class containing the id and the optionsCount from the array of StudentOptionEntry.

Each Student in the Student array is passed into the lambda expression for the SelectMany operator.

That lambda expression will then retrieve every StudentOptionEntry element whose id matches the id of the current Student.

The lambda expression's Select operator then creates an anonymous object containing the id and optionsCount members.

A sequence of zero or more anonymous objects for each passed Student is returned.

SelectMany operator then concatenates the sequence of sequences.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from   www .ja  va2 s.c o m*/
{
    static void Main(string[] args)
    {
        Student[] Students = Student.GetStudentsArray();
        StudentOptionEntry[] empOptions = StudentOptionEntry.GetStudentOptionEntries();

        var StudentOptions = Students
          .SelectMany(e => empOptions
                             .Where(eo => eo.id == e.id)
                             .Select(eo => new {
                                 id = eo.id,
                                 optionsCount = eo.optionsCount
                             }));

        foreach (var item in StudentOptions)
            Console.WriteLine(item);


    }
}
class Student
{
    public int id;
    public string firstName;
    public string lastName;

    public static ArrayList GetStudentsArrayList()
    {
        ArrayList al = new ArrayList();

        al.Add(new Student { id = 1, firstName = "Joe", lastName = "Ruby" });
        al.Add(new Student { id = 2, firstName = "Windows", lastName = "Python" });
        al.Add(new Student { id = 3, firstName = "Application", lastName = "HTML" });
        al.Add(new Student { id = 4, firstName = "David", lastName = "Visual" });
        al.Add(new Student { id = 101, firstName = "Kotlin", lastName = "Fortran" });
        return (al);
    }

    public static Student[] GetStudentsArray()
    {
        return ((Student[])GetStudentsArrayList().ToArray());
    }
}

class StudentOptionEntry
{
    public int id;
    public long optionsCount;
    public DateTime dateAwarded;

    public static StudentOptionEntry[] GetStudentOptionEntries()
    {
        StudentOptionEntry[] empOptions = new StudentOptionEntry[] {
      new StudentOptionEntry {
        id = 1,
        optionsCount = 2,
        dateAwarded = DateTime.Parse("1990/12/31") },
      new StudentOptionEntry {
        id = 2,
        optionsCount = 3000,
        dateAwarded = DateTime.Parse("1992/06/30")  },
      new StudentOptionEntry {
        id = 2,
        optionsCount = 3000,
        dateAwarded = DateTime.Parse("1991/01/01")  },
      new StudentOptionEntry {
        id = 3,
        optionsCount = 5000,
        dateAwarded = DateTime.Parse("1997/09/30") },
      new StudentOptionEntry {
        id = 2,
        optionsCount = 3000,
        dateAwarded = DateTime.Parse("2000/04/01")  },
      new StudentOptionEntry {
        id = 3,
        optionsCount = 7500,
        dateAwarded = DateTime.Parse("1998/09/30") },
      new StudentOptionEntry {
        id = 3,
        optionsCount = 7500,
        dateAwarded = DateTime.Parse("1998/09/30") },
      new StudentOptionEntry {
        id = 4,
        optionsCount = 2456,
        dateAwarded = DateTime.Parse("1997/12/31") },
      new StudentOptionEntry {
        id = 101,
        optionsCount = 2,
        dateAwarded = DateTime.Parse("1998/12/31") }
    };

        return (empOptions);
    }
}

Result

Related Topic