简体   繁体   English

使用Single()时,出现错误的“序列不包含任何元素”异常

[英]False “Sequence contains no elements” exception when using Single()

I seem to be having an issue with a false exception thrown when using .Single() in a Linq statement. 在Linq语句中使用.Single()时,我似乎遇到了引发错误异常的问题。 Checking the debugger I found that my list does contain the element I am looking for and the id field matches. 检查调试器后,我发现列表中确实包含我要查找的元素,并且id字段匹配。 The code that throws the exception is 引发异常的代码是

public ActionResult Details(int id = 0)
{
    FilterQueue filterqueue = db.FilterQueues.Single(f => f.FilterQueueId == id);

    if (filterqueue == null)
    {
        return HttpNotFound();
    }
    return View(filterqueue);
}

However, switching to a more verbose coding style works. 但是,切换到更详细的编码样式是可行的。 Like this: 像这样:

public ActionResult Details(int id = 0)
{
    FilterQueue filterqueue = null;
    foreach (var f in db.FilterQueues)
    {
        if (f.FilterQueueId == id) filterqueue = f;
    }
    if (filterqueue == null)
    {
        return HttpNotFound();
    }
    return View(filterqueue);
}

Funnily enough, if I let the code just run past the exception, it does grab the correct item to display the details of. 有趣的是,如果我让代码仅在异常之后运行,它将捕获正确的项以显示其详细信息。

Now, of course, I can switch the Single to SingleOrDefault and that works fine; 现在,当然,我可以将Single切换为SingleOrDefault,并且工作正常; However, since the first code block is what is written automatically by the framework, I want to understand what I am doing wrong. 但是,由于第一个代码块是框架自动编写的,所以我想了解自己在做错什么。 If I am making a systematic mistake, I would like to correct it the right way. 如果我犯了系统性的错误,我想以正确的方式纠正它。

I think you are looking for SingleOrDefault() . 我认为您正在寻找SingleOrDefault()

In your code, you are checking for null , when null could never be the result of Single(); 在您的代码中,您正在检查null ,当null永远不可能是Single()的结果时;

Here's what you want: 这就是您想要的:

public ActionResult Details(int id = 0)
{
    FilterQueue filterqueue = db.FilterQueues.SingleOrDefault(f => f.FilterQueueId == id);

    if (filterqueue == null)
    {
        return HttpNotFound();
    }

    return View(filterqueue);
}

In your first code, Single would never return null , If there are no matching elements found then it would throw an exception. 在您的第一个代码中, Single将永远不会返回null ,如果找不到匹配的元素,则它将引发异常。

"Sequence contains no element" “序列不包含任何元素”

You need SingleOrDefault which would return null if no element is found. 您需要SingleOrDefault ,如果找不到任何元素,它将返回null

Your second code block which is using the loop is not an equivalent representation of Single . 使用循环的第二个代码块不是Single的等效表示。 Enumerable.Single will throw the exception if more than one elements are returned matching the criteria. 如果返回多个符合条件的元素,则Enumerable.Single将引发异常。 Your loop, however is returning the last matched item. 但是,您的循环返回了最后一个匹配的项目。 There could be multiple items in the list but you will never know about it. 列表中可能有多个项目,但您永远不会知道。 Your loop is more similar to LastOrDefault 您的循环更类似于LastOrDefault

foreach (var f in db.FilterQueues)
{
    if (f.FilterQueueId == id) filterqueue = f;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM