What is the difference between Select and SelectMany in LINQ?
Hi everyone! I'm learning C# and LINQ, and I can't figure out the difference between the Select and SelectMany methods. They both seem to be used for data projection, but the result is somehow different.
I have a simple model: a Student class with a Name field (string) and a Courses list (List). I want to get all the courses that students are enrolled in. Here's what I tried:
```csharp
public class Student
{
public string Name { get; set; }
public List Courses { get; set; }
}
var students = new List
{
new Student { Name = "Ivan", Courses = new List { "Math", "Physics" } },
new Student { Name = "Maria", Courses = new List { "Chemistry", "Biology" } }
};
var selectResult = students.Select(s => s.Courses);
var selectManyResult = students.SelectMany(s
No answers yet.
Log in to leave an answer
Blogs
Salaries of JavaScript Developers in Russia 2024: Market Overview from Junior to Senior
How to Hide Structure Fields in JSON: Go for Beginners and Pros
Code review in Kotlin: what to check and how to give feedback
Deploying a PHP Application on VPS: A Step-by-Step Guide from A to Z