C# Linq Select Method

CsharpProgrammingServer Side Programming

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 }
raja
Published on 14-Sep-2018 15:14:59
Advertisements