Write a program in C# to Remove Items from List using remove function by passing object in LINQ Query


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class LinqExercise
  7. {
  8. static void Main(string[] args)
  9. {
  10.  
  11. List<string> list = new List<string>();
  12. list.Add("a");
  13. list.Add("b");
  14. list.Add("c");
  15. list.Add("d");
  16. list.Add("e");
  17.  
  18. var _result1 = from y in list
  19. select y;
  20. Console.Write("Here is the list of items : \n");
  21. foreach (var tchar in _result1)
  22. {
  23. Console.WriteLine("Char: {0} ", tchar);
  24. }
  25.  
  26. string newstr = list.FirstOrDefault(n => n == "c");
  27. list.Remove(newstr);
  28.  
  29.  
  30. var _result = from z in list
  31. select z;
  32. Console.Write("\nHere is the list after removing the item 'c' from the list : \n");
  33. foreach (var c in _result)
  34. {
  35. Console.WriteLine("Char: {0} ", c);
  36. }
  37.  
  38. Console.ReadLine();
  39. }
  40. }

Result

C# language techstudy.org

     





Related Topics




Popular Posts