我有以下 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 个值。当子查询跟随时这是不允许的=, !=, <, <= , >, >=
或者当子查询用作表达式时。
我想知道的是,在我的查询中,我在哪里做错了一些有时(大多数时候)会起作用的事情,然后很少返回此错误消息。如何正确预防这种情况的发生?