c# - 当结果为空时 LINQ 返回什么

标签 c# linq

我有一个关于 LINQ 查询的问题。通常查询返回 IEnumerable<T>类型。如果返回为空,则不确定它是否为空。我不确定以下是否 ToList()将抛出异常或只是一个空的 List<string>如果在 IEnumerable 中未找到任何内容结果?

   List<string> list = {"a"};
   // is the result null or something else?
   IEnumerable<string> ilist = from x in list where x == "ABC" select x;
   // Or directly to a list, exception thrown?
   List<string> list1 = (from x in list where x == "ABC" select x).ToList();

我知道这是一个很简单的问题,但是我暂时没有可用的VS。

最佳答案

它将返回一个空的枚举。它不会为空。你可以 sleep 的声音:)

关于c# - 当结果为空时 LINQ 返回什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1191919/

相关文章:

C# 读取 Windows 自动更新注册表返回的空值。

c# - 如何使用反射调用扩展方法?

c# - 如何从linq中的数据表中获取2列

c# - 如何在 LINQ 的表达式树中创建连接?

c# - 如何禁用我的 ASP.NET 应用程序的某些 API

c# - File.ReadLines() 行中的读取是否有最大值?

javascript - LINQ SingleOrDefault() 等效项

c# - 使用 Distinct 将 SQL 查询转换为 Linq (c#)

c# - 使用 HttpClient 调用 Web API 时出现的问题网络客户端 C#

c# - ASP.NET MVC3 : Reusable method which returns Database query results