简单的代码提取一条记录(之所以是单一的,是因为主键是一个设置为autonumber的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可能会有所帮助。
甚至似乎是一个常见问题:http://social.msdn.microsoft.com/Forums/en-US/afb292ca-3a61-4a60-8e18-830b92489016/method-single-not-supported-by-linq-to-entities?forum=adodotnetentityframework
解决方法,不知道是否有帮助,但来自上面的链接:
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/