提取单个记录的简单代码(它是单个记录是因为主键是一个设置为自动编号的 int):
using (var db = new DataClasses1DataContext())
{
var record = db.Projects.Single(x => x.ProjectID == projectID);
record.Deleted = true;
record.DeletedByUserID = MySession.Current.UserID;
record.DeletedOn = DateTime.Now;
db.SubmitChanges();
}
我不确定为什么,但在某些情况下,一旦它命中 db.SubmitChanges()
我们得到一个异常说明 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
但是当我“观看”记录时,我只得到了一条记录。可能是什么原因造成的?
最佳答案
使用 db.Projects.First(x => x.ProjectID == projectID);
代替 Single 可能会有所帮助。
解决方法,不知道是否有帮助,但是来自上面的链接:
public static TElement Single<TElement>
(this IQueryable<TElement> query)
{
if (query.Count() != 1)
{
throw new InvalidOperationException();
}
return query.First();
}
// Use it like this
Product prod = (from p in db.Product
where p.ProductID == 711
select p).Single();
关于c# - LINQ 提示子查询返回多于 1 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22870505/