Write a program in C# to remove a range of items from a list by passing the start index and number of elements to remove 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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
class LinqExercise21
{
    static void Main(string[] args)
    {
 
        List<string> list = new List<string>();
        list.Add("a");
        list.Add("b");
        list.Add("c");
        list.Add("d");
        list.Add("e");
        list.Add("f");
 
 
        var result = from y in list
                       select y;
        Console.Write("Here is the list of items : \n");
        foreach (var tchar in result)
        {
            Console.WriteLine("Char: {0} ", tchar);
        }
 
        list.RemoveRange(1, 4);
 
        var _result = from n in list
                      select n;
        Console.Write("\nHere is the list after removing the four items starting from the list : \n");
        foreach (var removeChar in _result)
        {
            Console.WriteLine("Char: {0} ", removeChar);
        }
 
        Console.ReadLine();
    }
}

Result

Write a program in C# to remove a range of items from a list by passing the start index and number of elements to remove in LINQ Query
Write a program in C# to remove a range of items from a list by passing the start index and number of elements to remove in LINQ Query

Leave a Comment