【发布时间】:2016-01-25 17:09:11
【问题描述】:
以下代码抛出 InvalidOperationException:序列包含多个元素。无论我是否有 Include 语句,都会发生错误。
long[] customerIDs; //Method parameter. Has valid values
var results = from x in DB.CycleCounts
//.Include(y => y.CustomerInventoryItem)
select x;
if (customerIDs != null && customerIDs.Length > 0)
{
results = from x in results
where customerIDs.Contains(x.CustomerInventoryItem.CustomerID)
select x;
}
var cycleCounts = await results.ToListAsync(); //throws InvalidOperationException
我正在使用 ASP5 RC1 (Core) 和 Entity Framework 7
【问题讨论】:
-
我猜你正试图在
cycleCounts中存储多个列表,其中cycleCounts是一维的。是否可以调试这一行并检查输出是否正确? -
代码示例是否正确?您在 if 块中访问 cycleCounts,但随后声明它。您的意思是“结果 = 来自结果中的 x...”?
-
对不起,我在粘贴代码后重命名了一些东西。我现在修好了,谢谢你抓住它。
-
您是否尝试在
customerIDs[]中返回任何customerId的结果? -
是的,customerIDs[] 是一个过滤器。如果 customerIDs 有一个值,我只想要循环计数谁的客户 ID 在数组中。我尝试使用 Any() 而不是 Contains() 但它抛出了相同的异常。
标签: c# entity-framework linq-to-sql ef-code-first asp.net-core