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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1191919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:26:56  来源:igfitidea点击:

What does LINQ return when the results are empty

c#linq

提问by David.Chu.ca

I have a question about LINQ query. Normally a query returns a IEnumerable<T>type. If the return is empty, not sure if it is null or not. I am not sure if the following ToList()will throw an exception or just a empty List<string>if nothing found in IEnumerableresult?

我有一个关于 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();

I know it is a very simple question, but I don't have VS available for the time being.

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

采纳答案by leppie

It will return an empty enumerable. It wont be null. You can sleep sound :)

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

回答by Paul van Brenk

.ToList returns an empty list. (same as new List() );

.ToList 返回一个空列表。(与 new List() 相同);

回答by Jimmy Chandra

It won't throw exception, you'll get an empty list.

它不会抛出异常,你会得到一个空列表。

回答by JP Alioto

var lst = new List<int>() { 1, 2, 3 };
var ans = lst.Where( i => i > 3 );

(ans == null).Dump();  // False
(ans.Count() == 0 ).Dump();  // True

(Dump is from LinqPad)

(转储来自LinqPad

回答by Spence

Other posts here have made it clear that the result is an "empty" IQueryable, which ToList() will correctly change to be an empty list etc.

此处的其他帖子已明确表明结果是“空”IQueryable,ToList() 将正确更改为空列表等。

Do be careful with some of the operators, as they will throw if you send them an empty enumerable. This can happen when you chain them together.

一定要小心一些操作符,因为如果你给它们发送一个空的枚举,它们会抛出异常。当您将它们链接在一起时,可能会发生这种情况。

回答by kay.one

In Linq-to-SQL if you try to get the first element on a query with no results you will get sequence contains no elementserror. I can assure you that the mentioned error is not equal to object reference not set to an instance of an object. in conclusion no, it won't return null since null can't say sequence contains no elementsit will always say object reference not set to an instance of an object;)

在 Linq-to-SQL 中,如果您尝试在没有结果的情况下获取查询的第一个元素,则会sequence contains no elements出错。我可以向您保证,上述错误不等于object reference not set to an instance of an object. 总之,不,它不会返回 null,因为 null 不能说它sequence contains no elements总是会说object reference not set to an instance of an object;)

回答by Noich

You can also check the .Any()method:

您还可以检查.Any()方法:

if (!YourResult.Any())

Just a note that .Anywill still retrieve the records from the database; doing a .FirstOrDefault()/.Where()will be just as much overhead but you would then be able to catch the object(s) returned from the query

只是一个.Any仍然会从数据库中检索记录的注释;做一个.FirstOrDefault()/.Where()将同样多的开销,但你将能够捕获从查询返回的对象