What does LINQ return when the result is null in C#?

王林
Release: 2023-09-10 16:57:10
forward
1027 people have browsed it

当 C# 中结果为空时 LINQ 返回什么?

Language Integrated Query (LINQ) is a set of Integrate query capabilities directly into the C# language.

You can use C# to create SQL Server databases, XML documents, ADO.NET data sets, and any collection of objects that support IEnumerable or generic IEnumerableinterface.

In Linq-to-SQL, if you try to get the first element in a query that has no results, you will The obtained sequence does not contain any 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();
   }
}
Copy after login

Output

0
Copy after login
Copy after login

Example

The Chinese translation is:

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();
   }
}
Copy after login

Output

0
Copy after login
Copy after login

The above is the detailed content of What does LINQ return when the result is null in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template