SqlException,因为Subquery返回的值超过1

时间:2011-07-28 15:41:00

标签: c# linq-to-sql linq-to-objects

我有以下LINQ查询,我用来构建一个结构到一个JavaScript网格库,这与这个例子无关,但我想我仍然会解释。

var output = myObjects.Select(
    p => new RowModel
    {
         ID = p.LeadUID,
         Cells =
             new CellCollection(fields,
                p.myDataDatas.Where(q => q.myField.ParentUID == null).Select(
                    q => new CellModel
                             {
                                 Value = q.Value,
                                 Name = q.myField.Description,
                                 Display = q.myField.Description
                             }).ToList()
                ,
                new CellModel
                    {
                        Name = "Campaign",
                        Display = "Campaign",
                        Value = p.Campaign.Name
                    }
                ,
                new CellModel
                    {
                        Name = "CampaignEnabled",
                        Display = "CampaignEnabled",
                        Value = p.Campaign.IsActive.ToString()
                    },
                new CellModel
                    {
                        Name = "Date Received",
                        Display = "Date Received",
                        Value = p.DateAdded.ToString()
                    }
                ,
                new CellModel
                    {
                        Name = "Is Valid",
                        Display = "Is Valid",
                        Value = BooleanMap[p.IsValid]
                    }
                ,
                new CellModel
                    {
                        Name = "Invalid Reason",
                        Display = "Invalid Reason",
                        Value = p.InvalidReason

                    }
                ,
                new CellModel
                    {
                        Name = "Is Returned",
                        Display = "Is Returned",
                        Value = BooleanMap[p.IsReturned]
                    }
                ,
                new CellModel
                    {
                        Name = "Return Reason",
                        Display = "Return Reason",
                        Value =
                            context.MYReturns.SingleOrDefault(
                                l => l.LeadUID == p.MyUID).ReturnReason
                    }
                ,
                new CellModel
                    {
                        Name = "Workflow",
                        Display = "Workflow",
                        Value =
                            context.Stages.SingleOrDefault(
                                s => s.LifecycleStageUID == p.LifecycleStageUID).
                            Name
                    }
                ,
                new CellModel
                    {
                        Name = "WorkflowEnabled",
                        Display = "WorkflowEnabled",
                        Value =
                            context.Stages.SingleOrDefault(
                                s => s.LifecycleStageUID == p.LifecycleStageUID).
                            IsActive.ToString()
                    }
                ,
                new CellModel
                    {
                        Name = "Status",
                        Display = "Status",
                        Value = p.MyStatus.Name
                    }
                ,
                new CellModel
                    {
                        Name = "StatusDeleted",
                        Display = "StatusDeleted",
                        Value = (p.MyStatus.Deleted).ToString()
                    }
                ,
                new CellModel
                    {
                        Name = "LeadSource",
                        Display = "Lead Source",
                        Value = MySourcesMap[p.AccountSourceUID].Name
                    }
                ,
                new CellModel
                    {
                        Name = "LeadSourceEnabled",
                        Display = "LeadSourceEnabled",
                        Value = AccountSoucesEnabledMap[p.AccountSourceUID].ToString()
                    }
                        )
                     }
            );
            var rows = output.ToList();
            return rows;

我希望我在代码中更改大多数变量的名称不会影响大局。

我遇到的问题是有时候我收到以下SQLException消息:

  

子查询返回的值超过1。当子查询跟在=, !=, <, <= , >, >=之后或子查询用作表达式时,不允许这样做。

我想知道的是,在我的查询中,我做错了什么,有时(大部分时间)工作,然后很少返回此错误消息。我怎样才能正确防止这种情况发生?

2 个答案:

答案 0 :(得分:1)

使用DataContext.Log属性显示查询生成的sql。当只有一个结果有效时,您很可能有一个子查询生成多个结果。例如,如果在子查询中返回多个结果,则以下sql将失败:

Select * from orders where customer_id = 
  (select customer_id from customer where name ='bob')

如果子查询返回的结果不止一个,则主查询中where子句的相等性是没有意义的。

您可能需要更改存储中某些数据列的唯一性,以确保在子查询中只返回一行。另一种方法是更改​​类,以便分配给的特定问题属性是集合而不是单个值。

答案 1 :(得分:0)

如果表达式返回多个项目,SingleOrDefault调用将抛出异常。也许你可以尝试使用FirstOrDefault,如果它只是你想要的前1名。