LINQ-序列不包含任何元素 [英] LINQ - Sequence contains no elements

查看:52
本文介绍了LINQ-序列不包含任何元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用如下LINQ查询.

I am using a LINQ query as below.

object.collection.where(t => t.id.Equals("2")).First();

我收到错误消息序列不包含任何元素".当结果不包含任何元素时,为什么结果会引发错误?如果找不到结果,是否应该不返回null?这就是使用SQL时发生的情况.

I am getting the error "Sequence contains no elements". Why does the result throw an error when the result contains no elements? Should it not return null when no results are found? That is what happens when using SQL.

推荐答案

object.collection.where(t => t.id.Equals("2")).FirstOrDefault();

这篇关于LINQ-序列不包含任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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