- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements