简体   繁体   English

LINQ抱怨子查询返回了超过1行

[英]LINQ complains that subquery returned more than 1 row

Simple code that pulls a single record ( it's single because the primary key is an int set as an autonumber ): 简单的代码提取一条记录(之所以是单一的,是因为主键是一个设置为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();
}

I'm not sure why but in some cases as soon as it hits db.SubmitChanges() we get an exception stating that Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 我不知道为什么,但是在某些情况下,一旦达到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. Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

But when I "Watch" the record I only am getting back one record. 但是,当我“观看”记录时,我只会得到一条记录。 What could be causing this? 是什么原因造成的?

That thing 那个东西

db.Projects.Single()

means that should be 100% only 1 value to return and there are many. 表示应为100%,仅返回1个值,并且有很多。

In that case use 在那种情况下使用

db.Projects.FirstOrDefault()

Or what is better check your data or select conditions. 或者最好检查您的数据或选择条件。

Enumerable.Single Method (IEnumerable) Enumerable.Single方法(IEnumerable)

And about this ... "But when I "Watch" the record I only am getting back one record. What could be causing this?" 关于这个问题……“但是当我“观看”唱片时,我只会找回一张唱片。这可能是什么原因造成的?”

Just check your table to see if you have duplicated entries or if you populate in other way your data context use db.Projects() to get all records and check it. 只需检查表以查看是否有重复的条目,或者是否以其他方式填充数据上下文,则使用db.Projects()获取所有记录并进行检查。

It may help to use db.Projects.First(x => x.ProjectID == projectID); 使用db.Projects.First(x => x.ProjectID == projectID);可能有所帮助db.Projects.First(x => x.ProjectID == projectID); instead of Single. 而不是单身。

It even seems to be a common problem: http://social.msdn.microsoft.com/Forums/en-US/afb292ca-3a61-4a60-8e18-830b92489016/method-single-not-supported-by-linq-to-entities?forum=adodotnetentityframework 它甚至似乎是一个常见问题: http : //social.msdn.microsoft.com/Forums/en-US/afb292ca-3a61-4a60-8e18-830b92489016/method-single-not-supported-by-linq-to -entities?forum = adodotnetentityframework

Way to solve, don't know if it helps but it's from above link: 解决方法,不知道是否有帮助,但来自上面的链接:

    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();

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

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