序列不包含元素? [英] Sequence contains no elements?

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

问题描述

我目前在两个地方使用单个查询从数据库中获取一行.

I'm currently using a single query in two places to get a row from a database.

BlogPost post = (from p in dc.BlogPosts
                 where p.BlogPostID == ID
                 select p).Single();

检索行以将数据放入文本框时查询正常,但在用于检索行以对其进行编辑并将其放回数据库时返回错误序列不包含元素".我不明白为什么它可能会在一个实例中找到合适的行,而在另一个实例中却找不到.

The query is fine when retrieving the row to put data in to the text boxes, but it returns an error "Sequence contains no elements" when used to retrieve the row in order to edit it and put it back in to the database. I can't understand why it might find an appropriate row in one instance but not another.

(使用 ASP.NET MVC 和 LINQ)

(Using ASP.NET MVC and LINQ)

推荐答案

来自修复 LINQ 错误:序列不包含元素":

当您收到 LINQ 错误序列不包含元素"时,这通常是因为您使用的是 First()Single() 命令而不是 FirstOrDefault()SingleOrDefault().

When you get the LINQ error "Sequence contains no elements", this is usually because you are using the First() or Single() command rather than FirstOrDefault() and SingleOrDefault().

这也可能是由以下命令引起的:

This can also be caused by the following commands:

  • FirstAsync()
  • SingleAsync()
  • Last()
  • LastAsync()
  • Max()
  • Min()
  • Average()
  • Aggregate()

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

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