Skip to main content

How to Turn a LINQ Query into an Array or a List in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A LINQ query doesn't hold any results: it runs again every time you read it. ToArray() and ToList() run it once and keep what came out.

Microsoft's reference says both methods force immediate query evaluation, and that you can append this method to your query in order to obtain a cached copy of the query results. The copy doesn't change when the source does.

Syntax​

Syntax
T[] <array> = <query>.ToArray();
List<T> <list> = <query>.ToList();
PartWhat it is
<query>Any sequence, such as the result of Select
ToArray()Runs it and returns an array, which has a fixed size
ToList()Runs it and returns a List<T>, which you can add to and remove from

The Example​

A console app called LinqToArray builds a query that uppercases three names, keeps its results in an array and a list, and then adds a fourth name to the source.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n LinqToArray
cd LinqToArray
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/LinqToArray/LinqToArray.csproj:
Determining projects to restore...
Restored /Users/you/source/LinqToArray/LinqToArray.csproj (in 47 ms).
Restore succeeded.

dotnet new console -n LinqToArray creates a folder named LinqToArray with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or LinqToArray.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the names, the query and the two copies:

Program.cs (the query and the copies)
List<string> names = ["Asha", "Ravi", "Mia"];

IEnumerable<string> query = names.Select(name => name.ToUpperInvariant());
string[] array = query.ToArray();
List<string> list = query.ToList();

Then change the source and print all three:

Program.cs (the change)
// The query runs again every time you read it; the array and the list keep what they got.
names.Add("Leo");

Console.WriteLine($"Query: {string.Join(", ", query)}");
Console.WriteLine($"Array: {string.Join(", ", array)}");
Console.WriteLine($"List: {string.Join(", ", list)}");

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Query: ASHA, RAVI, MIA, LEO
Array: ASHA, RAVI, MIA
List: ASHA, RAVI, MIA

macOS-style terminal window titled Terminal — LinqToArray showing dotnet run: the query now gives ASHA, RAVI, MIA, LEO, while the array and the list still hold ASHA, RAVI, MIA

The query ran again when it was printed, so it picked up Leo and returned four names. The array and the list kept the three names they got when they were made.

The Complete File​

macOS-style code window titled Program.cs — LinqToArray showing a list of three names, a Select query that uppercases them, ToArray and ToList on that query, a fourth name added to the source, and three Console.WriteLine calls

Program.cs
List<string> names = ["Asha", "Ravi", "Mia"];

IEnumerable<string> query = names.Select(name => name.ToUpperInvariant());
string[] array = query.ToArray();
List<string> list = query.ToList();

// The query runs again every time you read it; the array and the list keep what they got.
names.Add("Leo");

Console.WriteLine($"Query: {string.Join(", ", query)}");
Console.WriteLine($"Array: {string.Join(", ", array)}");
Console.WriteLine($"List: {string.Join(", ", list)}");

Common Mistakes​

MistakeWhat happensDo this instead
Reading a query several timesIt runs every time: reading it twice called the Select lambda 6 times for 3 namesCall ToList() once: reading the list twice afterwards made only the 3 calls ToList() needed
Trying to add to the arrayAn array has a fixed size: array.Add("LEO") doesn't build, with error CS1061: 'string[]' does not contain a definition for 'Add'Use ToList() when you need to add items

FAQ​

ToArray or ToList?​

ToList() when you'll add or remove items afterwards, ToArray() when the results stay as they are. Both run the query once.

Conclusion​

query.ToArray() and query.ToList() run a query once and keep a copy of its results, which won't follow later changes to the source. Use them when you read the results more than once or need them as they were at that moment.

Additional Resources​