Q:

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

belongs to collection: All LINQ programs in C# with examples

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

Here is the list of items : 

Char: a 

Char: b 

Char: c 

Char: d 

Char: e 

Char: f 

Here is the list after removing the four items starting from the list : 

Char: a 

Char: f

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a program in C# to generate a Cartesian Prod... >>
<< Write a program in C# to Remove Items from List us...