我有一个关于 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/