w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: LINQ : Remove Items from List by passing filters

C# Sharp LINQ : Exercise-19 with Solution

Write a program in C# Sharp to Remove Items from List by passing filters.

Sample Solution:-

C# Sharp Code:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  

class  LinqExercise19
    {  
        static void Main(string[] args)  
      {  
           
            List<string> listOfString = new List<string>();  
            listOfString.Add("m");  
            listOfString.Add("n");  
            listOfString.Add("o");  
            listOfString.Add("p");  
            listOfString.Add("q");  
			
			
            Console.Write("\nLINQ : Remove Items from List by passing filters  : "); 
            Console.Write("\n--------------------------------------------------\n");
            
            var _result1 = from y in listOfString  
            select y; 
            Console.Write("Here is the list of items : \n");
            foreach(var tchar in _result1)  
            {  
                Console.WriteLine("Char: {0} ", tchar);  
            } 

            listOfString.RemoveAll(en => en == "q"); 
 
  
            var _result = from z in listOfString  
            select z;  
  	        Console.Write("\nHere is the list after removing item 'q' from the list : \n");
            foreach(var rChar in _result)  
            {  
                Console.WriteLine("Char: {0} ", rChar);  
            }  
  
            Console.ReadLine();  
        }  
    }
	

Sample Output:

LINQ : Remove Items from List by passing filters  :                                                           
--------------------------------------------------                                                            
Here is the list of items :                                                                                   
Char: m                                                                                                     
Char: n                                                                                                     
Char: o                                                                                                     
Char: p                                                                                                     
Char: q                                                                                                     
Here is the list after removing item 'q' from the list :                                                      
Char: m                                                                                                     
Char: n                                                                                                     
Char: o                                                                                                     
Char: p 

Flowchart:

Flowchart: LINQ : Remove Items from List by passing filters

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to Remove Items from List by creating an object internally by filtering.
Next: Write a program in C# Sharp to Remove Items from List by passing the item index.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming