Computer >> Máy Tính >  >> Lập trình >> C#

Làm cách nào chúng ta có thể cập nhật các giá trị của một tập hợp bằng LINQ trong C #?

Nếu tập hợp là một Danh sách, thì chúng tôi có thể sử dụng phương thức mở rộng ForEach, phương thức này có sẵn như một phần của LINQ.

Ví dụ

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; }
   }
}

Đầu ra

Đầu ra của đoạn mã trên là

Fruit Details Before Update. Apple, Small
Fruit Details Before Update. Orange, Small
Fruit Details After Update. Apple, Large
Fruit Details After Update. Orange, Large

Nếu chúng ta muốn cập nhật các mục trong danh sách dựa trên một điều kiện, chúng ta có thể sử dụng mệnh đề Where ().

Ví dụ

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; }
   }
}

Đầu ra

Đầu ra của đoạn mã trên là

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

Ở trên, chúng tôi chỉ lọc những trái cây có kích thước nhỏ và cập nhật các giá trị. Vì vậy, mệnh đề where lọc bản ghi dựa trên một điều kiện.