SqlException because Subquery returned more than 1 value
I have the following LINQ query that I am using to construct a structure to stuff into a JavaScript grid library which is irrelevant for this example, but I figured I would still explain that.
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 Retur开发者_开发问答ned",
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;
I would like to expect that my changing the names of most of my variables in the code will not affect the big picture.
My problem that I am facing is SOMETIMES I am getting the following SQLException
message:
Subquery returned more than 1 value. This is not permitted when the subquery follows
=, !=, <, <= , >, >=
or when the subquery is used as an expression.
What I am wondering, is where in my query am I doing something wrong that would sometimes(most of the time) work, and then very rarely returns this error message. How can I correctly prevent this from happening?
use the DataContext.Log property to display the sql generated by your query. You most likely have a sub-query that is generating more than one result when only one result is valid. For example the following sql will fail if more than one result is returned in the sub-query:
Select * from orders where customer_id =
(select customer_id from customer where name ='bob')
The equality of the where clause in the main query makes no sense if there is more than one result returned from the sub-query.
You may need to alter the uniqueness of some columns of data in your storage in order to ensure that only one row is returned in the sub-query. Another alternative is to alter your class so that the specific problem property being assigned to is a collection instead of a single value.
The SingleOrDefault
call will throw an exception if the expression returns more than one item. Maybe you could try and use FirstOrDefault
if its just the Top 1 you want.
精彩评论