C# LINQ 查询问题,序列不包含任何元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9907963/
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
LINQ Query Issue, Sequence contains no elements
提问by Naveed
I'm trying to update a single record in a table, but when I run .Firstordefault(), I get the error: "Object reference not set to an instance of an object.", and if use it with .First(), I get "Sequence contains no elements".
我正在尝试更新表中的单个记录,但是当我运行时.Firstordefault(),出现错误:“对象引用未设置为对象的实例。”,如果将它与 一起使用.First(),我会得到“序列不包含任何元素”。
Using it another place, its working fine, but this time its causing errors.
在另一个地方使用它,它工作正常,但这次它会导致错误。
Here's the code:
这是代码:
public class AllownceDetails
{
public int ta_id{get;set;}
public int tvrid{get;set;}
public DateTime ofDate{get;set;}
public string status{get;set;}
public string userid {get;set;}
}
//Update Method
public void Update(AllownceDetails Allowncedtl)
{
var ta = (from a in ce.tbl_tvrallownce
where a.tvrid == Allowncedtl.tvrid
//error: Sequence contains no elements
select a).SingleOrDefault();
ta.status = Allowncedtl.status;
//error:Object reference not set to an instance of an object
ce.SaveChanges();
}
回答by Jon Skeet
All that means is that your query isn't matching anything. Presumably Allowncedtl.tvridis an ID which doesn't match anything in the database. You shouldn'tassume that SingleOrDefaultwill return a non-null value. Only use SingleOrDefaultwhen you're expectingthat there may be no values - and cope with that. If a failure to find a value indicates a bug, you should use Single(or perhaps First) instead.
这意味着您的查询不匹配任何内容。大概Allowncedtl.tvrid是一个与数据库中的任何内容都不匹配的 ID。您不应该假设它SingleOrDefault会返回一个非空值。仅SingleOrDefault在您预计可能没有值时使用 - 并处理它。如果未能找到值表明存在错误,则应使用Single(或可能First)代替。
We can't tell anything about what the underlyingcause of your error is - you should log which ID you were looking for, work out whyyou were looking for that, and then check it in the database.
我们无法告诉您错误的根本原因是什么- 您应该记录您正在寻找的 ID,找出您为什么要寻找它,然后在数据库中检查它。
回答by David
The query must not be returning any data. Run a profiler on the SQL database to see the physical query being executed and try to execute it manually against the database to see what the data looks like. You probably have to adjust the query (or the data) to get the results you're looking for.
查询不得返回任何数据。在 SQL 数据库上运行分析器以查看正在执行的物理查询,并尝试针对数据库手动执行它以查看数据的外观。您可能必须调整查询(或数据)才能获得所需的结果。
"Sequence contains no elements" is basically LINQ's way of telling you that you're trying to reference an element from a list that doesn't have anything. So calls to things like .First()or .Single()can't find anything, hence the error.
“序列不包含任何元素”基本上是 LINQ 告诉您您正在尝试从没有任何内容的列表中引用元素的方式。所以调用诸如.First()或.Single()找不到任何东西,因此出现错误。
When you change the calls to something like .FirstOrDefault()or .SingleOrDefault()then it will go with "default" value for that type, and for reference types the default is null. So if you set something to nulland then try to call a method on it, you'll get object reference not set to an instance of an object.
当您将调用更改为类似.FirstOrDefault()or 时.SingleOrDefault(),它将使用该类型的“默认”值,而对于引用类型,默认值为null. 因此,如果您将某些内容设置为null然后尝试对其调用方法,您将获得object reference not set to an instance of an object.
回答by Adrian Iftode
The Singlemethod throws this exception when there are no elements in list(query) or there are multiple elements.
当 list(query) 中没有元素或有多个元素时,Single方法会抛出此异常。
The SingleOrDefaultmethod throws an exception when there are multiple elements in the list. Returns null when there are no elements.
当列表中有多个元素时,SingleOrDefault方法会引发异常。当没有元素时返回 null。
The FirstOrDefaultmethod return the first item in the list or null. There are no exceptions.
该FirstOrDefault方法在列表或NULL返回的第一个项目。没有例外。
Use it and the check the reference for null
使用它并检查引用是否为空
if (ta != null )
{
ta.status = Allowncedtl.status;
ce.SaveChanges()
}
The source will always be an object, so no worries about it in your case.
源将始终是一个对象,因此在您的情况下不必担心。

