Home  >  Article  >  Backend Development  >  What does LINQ return when the result is null in C#?

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

王林
王林forward
2023-09-10 16:57:10993browse

当 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 IEnumerable8742468051c85b06f0a0af9e3e506b5cinterface.

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

Output

0

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

Output

0

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Double array in C#?Next article:Double array in C#?