[英]The Result of a Query Cannot be Enumerated More than Once (C#) (Entity Framework) (Stored Procedure)
When I get to db.GetLabelComponentsByLabelID(LabelIDs.ElementAt(i).Value.ToString()).ToList()
当我到达
db.GetLabelComponentsByLabelID(LabelIDs.ElementAt(i).Value.ToString()).ToList()
I get the following exception: The Result of a Query Cannot be Enumerated More than Once
我收到以下异常:
The Result of a Query Cannot be Enumerated More than Once
I tried to change LabelComponents by calling ToList()
like this answer suggested.我试图通过像这个答案建议的那样调用
ToList()
来更改 LabelComponents。
long? GroupID = db.GetGroupIDByName("PrintTest").SingleOrDefault();
ObjectResult<long?> LabelIDs = db.GetLabelIDSFromGroupingsByGroupID(GroupID.ToString());
for (int i = 0; i < LabelIDs.Count(); i++)
{
var LabelComponents = db.GetLabelComponentsByLabelID(LabelIDs.ElementAt(i).Value.ToString()).ToList();
List<Component> Label = new List<Component>();
for(int j = 0; j < LabelComponents.Count(); j++)
{
....
....
You need ToList
on the first db call because you are enumerating the LabelIDs
value multiple times.您在第一次db 调用时需要
ToList
,因为您要多次枚举LabelIDs
值。 LabelIDs.Count
runs the query the first time, then LabelIDs.ElementAt
runs it again later on. LabelIDs.Count
第一次运行查询,然后LabelIDs.ElementAt
稍后再次运行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.