Mid Collections

How do you add elements to a collection using LINQ in C#?

LINQ itself doesn’t modify collections directly but produces new collections based on

queries.

You typically combine LINQ with collection methods to add elements, for example:

Follow:

var evenNumbers = new List<int> { 2, 4, 6 };

var allNumbers = new List<int> { 1, 2, 3, 4, 5, 6 };

var combined = allNumbers.Where(n => n % 2 == 0).ToList(); //

Filters even numbers

If you want to add LINQ results to a collection:

List<int> filteredNumbers = allNumbers.Where(n => n % 2 ==

0).ToList();

More from C# Programming Tutorial

All questions for this course