LINQ ToArray() Method

  Total Views : 244
  Comments : 0
 
  Keywords : Linq toarray() operator / method in c# / vb.net with example, How to use linq toarray() method to convert collection items to array with example, Linq convert list to array using toarray()

Here we will learn LINQ ToArray() method / operator with example in c#, vb.net and how to use LINQ ToArray() method to convert list / collection items to array list in method syntax and query syntax using c#, vb.net with examples.

LINQ ToArray Operator

In LINQ ToArray() operator is used to convert the input elements in collection to an Array. 

Syntax of LINQ ToArray() Operator

Following is the syntax of using LINQ ToArray() operator to convert collection to an array.

 

C# Code

 

string[] countryarray = countries.ToArray();

VB.NET Code

 

Dim countryarray As String() = countries.ToArray()

If you observe above syntax we are converting “countries” collection to an array.

Example of LINQ ToArray() Operator in Method Syntax

Following is the example of using LINQ ToArray() operator in method syntax to convert input collection items to new array.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace LINQExamples

{

classProgram

{

staticvoid Main(string[] args)

{

string[] countries = { "US", "UK","India", "Russia", "China", "Australia", "Argentina" };

string[] countryarray = countries.ToArray();

foreach (string s in countryarray)

{

Console.WriteLine(s);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim countries As String() = {"US", "UK", "India", "Russia", "China", "Australia", "Argentina"}

Dim countryarray As String() = countries.ToArray()

ForEach s As String In countryarray

Console.WriteLine(s)

Next

Console.ReadLine()

EndSub

End Module

If you observe above example we have a List of type string “countries”. By using ToArray () method we converted “countries” List to an array. To access these elements we have iterated the array in a foreach loop and displayed on the screen.

Output of LINQ ToArray() in Method Syntax Example

Following is the result of LINQ ToArray() operator in method syntax example.

 

US

UK

India

Russia

China

Australia

Argentina

LINQ ToArray() Operator in Query Syntax Example

Following is the example of using LINQ ToArray() operator in query syntax.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace LINQExamples

{

classProgram

{

staticvoid Main(string[] args)

{

string[] countries = { "US", "UK","India", "Russia", "China", "Australia", "Argentina" };

string[] countryarray = (from x in countries select x).ToArray();

foreach (string s in countryarray)

{

Console.WriteLine(s);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim countries As String() = {"US", "UK", "India", "Russia", "China", "Australia", "Argentina"}

Dim countryarray As String() = (From x In countries).ToArray()

ForEach s As String In countryarray

Console.WriteLine(s)

Next

Console.ReadLine()

EndSub

End Module

Output of LINQ ToArray() in Query Syntax Example

Following is the result of LINQ ToArray() in query syntax example.

 

US

UK

India

Russia

China

Australia

Argentina

This is how we can use LINQ ToArray to convet given collection items to array list using method syntax and query syntax based on our rrquirements.

 
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User