Skip to content
Loading
How to Update an item in a list by LINQ
  • Subhendu De
    Yes you can but you need to write the extension method for it. Dont worry, Troy Magennis had written it. I used that extension method. The link is
     
    http://www.hookedonlinq.com/UpdateOperator.ashx
     
    Please go through the code. If you need any other info, please let me know.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Data.Linq;  
    6. using System.IO;  
    7. using System.Threading;  
    8. using System.Data.SqlClient;  
    9.   
    10. namespace PracticeConsole  
    11. {  
    12.     class Program  
    13.     {  
    14.         static void Main(string[] args)  
    15.         {  
    16.             Student[] _arrStudents = new Student[]  
    17.                                     {  
    18.                                         new Student("AAA","1","A"),  
    19.                                         new Student("BBB","2","B"),   
    20.                                         new Student("CCC","3","C"),   
    21.                                         new Student("DDD","4","D"),   
    22.                                         new Student("EEE","5","E"),   
    23.                                         new Student("FFF","6","F"),   
    24.                                         new Student("GGG","7","G"),   
    25.                                         new Student("HHH","8","H"),   
    26.                                         new Student("III","9","I")  
    27.                                     };  
    28.   
    29.             Console.WriteLine("Before update");  
    30.   
    31.             foreach (Student _student in _arrStudents)  
    32.             {  
    33.                 Console.WriteLine(String.Format("{0},{1},{2}", _student.Name, _student.Standard, _student.Status));  
    34.             }  
    35.   
    36.             var query = (from stud in _arrStudents  
    37.                          where stud.Name == "BBB"  
    38.                          select stud)  
    39.                         .Update(st => { st.Standard = "0"; st.Status = "X"; });  
    40.   
    41.             Console.WriteLine("After update");  
    42.   
    43.             foreach (Student _student in _arrStudents)  
    44.             {  
    45.                 Console.WriteLine(String.Format("{0},{1},{2}",_student.Name,_student.Standard,_student.Status));  
    46.             }  
    47.         }  
    48.     }  
    49.   
    50.     class Student  
    51.     {  
    52.         public string Name { getset; }  
    53.         public string Standard { getset; }  
    54.         public string Status { getset; }  
    55.           
    56.         public Student(string name, string standard, string status)  
    57.         {  
    58.             this.Name = name;  
    59.             this.Standard = standard;  
    60.             this.Status = status;  
    61.         }          
    62.     }  
    63.   
    64.     public static class UpdateExtensions  
    65.     {  
    66.         public delegate void Func(TArg0 element);  
    67.           
    68.         public static int Update(this IEnumerable source, Func update)  
    69.         {  
    70.             if (source == nullthrow new ArgumentNullException("source");  
    71.             if (update == nullthrow new ArgumentNullException("update");  
    72.             if (typeof(TSource).IsValueType)  
    73.                 throw new NotSupportedException("value type elements are not supported by update.");  
    74.   
    75.             int count = 0;  
    76.             foreach (TSource element in source)  
    77.             {  
    78.                 update(element);  
    79.                 count++;  
    80.             }  
    81.             return count;  
    82.         }  
    83.     }  
    84. }
    0