Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can we update the values of a collection using LINQ in C#?
LINQ provides several approaches to update collection values in C#. While LINQ is primarily designed for querying data, you can combine it with other methods to modify collection elements efficiently.
Using ForEach with List Collections
The List<T> class provides a ForEach method that can be used to update all elements in the collection −
using System;
using System.Collections.Generic;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
List<Fruit> fruits = new List<Fruit> {
new Fruit {
Name = "Apple",
Size = "Small"
},
new Fruit {
Name = "Orange",
Size = "Small"
}
};
foreach(var fruit in fruits) {
Console.WriteLine($"Fruit Details Before Update. {fruit.Name}, {fruit.Size}");
}
fruits.ForEach(fruit => { fruit.Size = "Large"; });
foreach (var fruit in fruits) {
Console.WriteLine($"Fruit Details After Update. {fruit.Name}, {fruit.Size}");
}
}
}
public class Fruit {
public string Name { get; set; }
public string Size { get; set; }
}
}
The output of the above code is −
Fruit Details Before Update. Apple, Small Fruit Details Before Update. Orange, Small Fruit Details After Update. Apple, Large Fruit Details After Update. Orange, Large
Using Where() for Conditional Updates
To update collection items based on specific conditions, combine Where() with a foreach loop −
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
List<Fruit> fruits = new List<Fruit> {
new Fruit {
Name = "Apple",
Size = "Small"
},
new Fruit {
Name = "Orange",
Size = "Small"
},
new Fruit {
Name = "Mango",
Size = "Medium"
}
};
foreach(var fruit in fruits) {
Console.WriteLine($"Fruit Details Before Update. {fruit.Name}, {fruit.Size}");
}
foreach (var fruit in fruits.Where(w => w.Size == "Small")) {
fruit.Size = "Large";
}
foreach (var fruit in fruits) {
Console.WriteLine($"Fruit Details After Update. {fruit.Name}, {fruit.Size}");
}
}
}
public class Fruit {
public string Name { get; set; }
public string Size { get; set; }
}
}
The output of the above code is −
Fruit Details Before Update. Apple, Small Fruit Details Before Update. Orange, Small Fruit Details Before Update. Mango, Medium Fruit Details After Update. Apple, Large Fruit Details After Update. Orange, Large Fruit Details After Update. Mango, Medium
Using Select() to Create Updated Collections
For immutable updates, use Select() to create a new collection with updated values −
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
Console.WriteLine("Original numbers:");
foreach (var num in numbers) {
Console.Write(num + " ");
}
Console.WriteLine();
var updatedNumbers = numbers.Select(x => x * 2).ToList();
Console.WriteLine("Updated numbers (doubled):");
foreach (var num in updatedNumbers) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Original numbers: 1 2 3 4 5 Updated numbers (doubled): 2 4 6 8 10
Comparison of Update Methods
| Method | Modifies Original | Best For |
|---|---|---|
| List.ForEach() | Yes | Updating all elements in a List |
| Where() + foreach | Yes | Conditional updates to existing objects |
| Select() + ToList() | No | Creating new collections with transformed values |
Conclusion
LINQ can be effectively combined with traditional loops to update collection values. Use ForEach() for bulk updates, Where() with loops for conditional updates, and Select() when you need to create new collections with transformed data while preserving the original.
