Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What does LINQ return when the results are empty in C#?
Language-Integrated Query (LINQ) is a set of technologies that integrates query capabilities directly into the C# language. When LINQ queries return no results, the behavior depends on which LINQ method you use.
LINQ queries can be written for SQL Server databases, XML documents, ADO.NET Datasets, and any collection that supports IEnumerable or IEnumerable<T>. Understanding what happens when queries return empty results is crucial for avoiding runtime exceptions.
Common LINQ Methods and Empty Results
| LINQ Method | Empty Result Behavior |
|---|---|
ToList() |
Returns an empty List<T>
|
ToArray() |
Returns an empty array |
First() |
Throws InvalidOperationException
|
FirstOrDefault() |
Returns default value (null for reference types) |
Count() |
Returns 0 |
Using ToList() with Empty Results
The ToList() method returns an empty list when no elements match the query criteria −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
List<string> list = new List<string> { "apple", "banana", "cherry" };
IEnumerable<string> result = list.Where(x => x == "orange").ToList();
Console.WriteLine("Count: " + result.Count());
Console.WriteLine("Result is empty: " + (!result.Any()));
foreach (var item in result) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Count: 0 Result is empty: True
Using First() vs FirstOrDefault()
The First()FirstOrDefault() returns the default value −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
// FirstOrDefault returns 0 (default for int) when no match
int result1 = numbers.Where(x => x > 10).FirstOrDefault();
Console.WriteLine("FirstOrDefault result: " + result1);
// First would throw an exception, so we'll check if any exist first
bool hasResult = numbers.Where(x => x > 10).Any();
Console.WriteLine("Has results: " + hasResult);
// Safe approach for reference types
List<string> names = new List<string> { "John", "Jane" };
string name = names.Where(x => x == "Bob").FirstOrDefault();
Console.WriteLine("Name result: " + (name ?? "null"));
}
}
The output of the above code is −
FirstOrDefault result: 0 Has results: False Name result: null
Using ToArray() and Count() with Empty Results
Similar to ToList(), other LINQ methods handle empty results gracefully −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
List<int> numbers = new List<int> { 10, 20, 30 };
// ToArray returns empty array
int[] evenNumbers = numbers.Where(x => x % 2 == 1).ToArray();
Console.WriteLine("Array length: " + evenNumbers.Length);
// Count returns 0
int count = numbers.Where(x => x > 100).Count();
Console.WriteLine("Count: " + count);
// Any returns false
bool hasLargeNumbers = numbers.Where(x => x > 100).Any();
Console.WriteLine("Has large numbers: " + hasLargeNumbers);
}
}
The output of the above code is −
Array length: 0 Count: 0 Has large numbers: False
Best Practices for Handling Empty Results
Use
FirstOrDefault()instead ofFirst()to avoid exceptions.Use
Any()to check if results exist before processing.Use
ToList()orToArray()for safe iteration over potentially empty results.Always check for
nullwhen usingFirstOrDefault()with reference types.
Conclusion
LINQ methods handle empty results differently: ToList() and ToArray() return empty collections, Count() returns zero, while First() throws exceptions and FirstOrDefault() returns default values. Choose the appropriate method based on your needs to avoid runtime errors.
