结果为空时LINQ返回什么 [英] What does LINQ return when the results are empty

查看:35
本文介绍了结果为空时LINQ返回什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 LINQ 查询的问题.通常,查询返回 IEnumerable 类型.如果返回为空,不确定它是否为空.如果在 IEnumerable 结果中没有发现任何内容,我不确定以下 ToList() 是否会抛出异常或只是一个空的 List ?

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 IEnumerable result?

   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.

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

推荐答案

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

It will return an empty enumerable. It won't be null. You can sleep sound :)

这篇关于结果为空时LINQ返回什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆