LINQ ToList Method

  Total Views : 260
  Comments : 0
 
  Keywords : How to use linq tolist() method / conversion operator with example, Linq tolist method to convert collection items to new list with example, Linq convert query results to tolist with example

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

LINQ ToList() Operator

In LINQ ToList operator takes the elements from the given source and it returns a new List. So that means the input would be converted to type List.

Syntax of LINQ ToList() Operator

Following is the syntax of using LINQ ToList() to convert input collection to list.

 

C# Code

 

List<string> result = countries.ToList();

VB.NET Code

 

Dim result As List(Of String) = countries.ToList()

If you observe above syntax we are converting “countries” collection to list using LINQ ToList() operator.

LINQ ToList() Operator Example in Method Syntax

Following is the example of using LINQ ToList() to convert input collection to List in method 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" };

List<string> result = countries.ToList();

foreach (string s in result)

{

Console.WriteLine(s);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

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

Dim result As List(Of String) = countries.ToList()

ForEach s As String In result

Console.WriteLine(s)

Next

Console.ReadLine()

EndSub

EndModule

If you observe above example we are converting countries collection to List by using LINQ ToList () method. 

Output of LINQ ToList() Operator Example

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

 

US

UK

India

Russia

China

Australia

Argentina

Example of LINQ ToList() in Query Syntax

Following is the example of using LINQ ToList() 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" };

List<string> result = (from x in countries select x).ToList();

 

foreach (string s in result)

{

Console.WriteLine(s);

}

Console.ReadLine();

}

}

}

VB.NET

 

Module Module1

Sub Main()

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

Dim result As List(Of String) = (From x In countries).ToList()

ForEach s As String In result

Console.WriteLine(s)

Next

Console.ReadLine()

EndSub

EndModule

Output of LINQ ToList() in Query Syntax

Following is the result of using LINQ ToList() operator in query syntax.

 

US

UK

India

Russia

China

Australia

Argentina

This is how we can use LINQ ToList() operator / method to convert given collection items to new list.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User