LINQ to String Array

  Total Views : 139
  Comments : 0
 
  Keywords : Linq to string arrays with example in c# / vb.net, Use linq queries on string arrays in c# / vb.net, Linq query to check string array contains required elements in c# / vb.net with example

Here we will learn LINQ to String Array in c#, vb.net with example and how to use linq query to get string array elements which starts with word in c#, vb.net with example.

LINQ to String Array

LINQ to String Array means writing LINQ queries on string array to get required data. If we use LINQ queries on string arrays we can get required elements easily without write much code.

Syntax of LINQ to String Arrays

Following is the syntax of writing LINQ queries on string arrays to get required elements from array collection.

 

C# Code

 

   IEnumerable<string> result = from a in arr

                                                    select a;

VB.NET Code

 

Dim result As IEnumerable(Of String) = From a In arr

If you observe above syntax we written LINQ query to get data from “arr” string array.

Example of LINQ to String Array

Following is the example of LINQ to String Array to get elements from the string array sequence where name starts with “S”.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Linqtutorials

{

class Program

{

static void Main(string[] args)

{

string[] arr = { "Suresh", "Rohini", "Praveen", "Sateesh" };

IEnumerable<string> result = from a in arr

where a.ToLowerInvariant().StartsWith("s")

select a;

foreach (string item in result)

{

Console.WriteLine(item);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim arr As String() = {"Suresh", "Rohini", "Praveen", "Sateesh"}

Dim result As IEnumerable(Of String) = From a In arr Where a.ToLowerInvariant().StartsWith("s")

For Each item As String In result

Console.WriteLine(item)

Next

Console.ReadLine()

End Sub

End Module

If you observe above code, we used LINQ query on “arr” string array to get elements whose name starts with “s”. Here we used ToLowerInvariant() property to convert string array elements to lowercase and compare because LINQ queries are case sensitive.

Output of LINQ to String Array Example

Following is the result of LINQ to String Array Example.

 

Suresh

Sateesh

This is how we can use LINQ queries with string arrays to get required data.

 

PREVIOUS

LINQ to Strings
 
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User