
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How can we update the values of a collection using LINQ in C#?
If the collection is a List, then we can make use of ForEach extension method which is available as part of LINQ.
Example
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}"); } Console.ReadLine(); } } public class Fruit { public string Name { get; set; } public string Size { get; set; } } }
Output
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
If we want to update the list items based on a condition, we can make use of Where() clause.
Example
using System; using System.Collections.Generic; using System.Linq; namespace DemoApplication { class Program { static void Main(string[] args) { IEnumerable<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}"); } Console.ReadLine(); } } public class Fruit { public string Name { get; set; } public string Size { get; set; } } }
Output
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
In the above we are filtering only the fruits having small size and updating the values. So, the where clause filters the record based on a condition.
- Related Articles
- How can we update values in a MySQL table?
- How can we update any value in MySQL view as we can update the values in MySQL table?
- How can we update the values in one MySQL table by using the values of another MySQL table?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- How can we update MySQL table after padding a string with the values of the column?
- How can we update MySQL table after removing a particular string from the values of column?
- How can we use existing values of the rows to provide new values in the SET clause of UPDATE statement?
- How can we update a record in MongoDB?
- How can you update certain values in a table in MySQL using Python?
- How to update MongoDB collection using $toLower?
- How to Update multiple documents in a MongoDB collection using Java?
- How can I update the boolean values in MySQL?
- How can we change MySQL user password by using UPDATE statement?
- How can we return multiple values from a function in C/C++?
- How to flatten a list using LINQ C#?

Advertisements