
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What does LINQ return when the results are empty in C#?
Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language.
You can write LINQ queries in C# for SQL Server databases, XML documents, ADO.NET Datasets, and any collection of objects that supports IEnumerable or the generic IEnumerable<T> interface.
In Linq-to-SQL if you try to get the first element on a query with no results you will get sequence contains no elements error
ToList returns an empty list
Example
class Program{ public static void Main(){ List<string> list = new List<string> { "a" }; IEnumerable<string> ilist = list.Where(x => x == "ABC").ToList(); System.Console.WriteLine(ilist.Count()); foreach (var item in ilist){ System.Console.WriteLine(item); } Console.ReadLine(); } }
Output
0
Example
class Program{ public static void Main(){ List<int> list = new List<int> { 1 }; IEnumerable<int> ilist = list.Where(x => x == 3).ToList(); System.Console.WriteLine(ilist.Count()); foreach (var item in ilist){ System.Console.WriteLine(item); } Console.ReadLine(); } }
Output
0
- Related Articles
- What are union, intersect and except operators in Linq C#?
- What does the method empty() do in java?
- What value does read() method return when it reaches the end of a file in java?
- What does "return@" mean in Kotlin?
- What is the difference between Select and SelectMany in Linq C#?
- What is the difference between Last() and LastOrDefault() in Linq C#?
- What is the difference between All and Any in C# Linq?
- Return an empty masked array of the given shape where all the data are masked in Numpy
- C# Linq Where Method
- C# Linq Intersect Method
- C# Linq FirstorDefault Method
- C# Linq Reverse Method
- C# Linq Select Method
- C# Linq SelectMany Method
- C# Linq Skip() Method

Advertisements