C# Linq Select Method


Use the Select method to modify the elements in an array.

The following is our string array.

string[] stationery = { "diary", "board", "pencil", "whiteboard" };

The Select method also specifies Lambda Expressions as shown below −

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      string[] stationery = { "diary", "board", "pencil", "whiteboard" };
      var res = stationery.AsQueryable().Select((item, index) => new { result = item.Substring(0, index + 4) });
      foreach (var str in res) {
         Console.WriteLine("{0}", str);
      }
   }
}

Output

{ result = diar }
{ result = board }
{ result = pencil }
{ result = whitebo }

Updated on: 23-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements