Select Many example
Select Many :
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Sample list of students, each with a list of courses
List students = new List
{
new Student { Name = "Alice", Courses = new List { "Math", "History" } },
new Student { Name = "Bob", Courses = new List { "Science", "English" } },
new Student { Name = "Charlie", Courses = new List { "Math", "Art" } }
};
// Using SelectMany to flatten the list of courses
var allCourses = students.SelectMany(student => student.Courses);
// Displaying the result
Console.WriteLine("All Courses:");
foreach (var course in allCourses)
{
Console.WriteLine(course);
}
}
}
class Student
{
public string Name { get; set; }
public List Courses { get; set; }
}
Comments
Post a Comment