简体   繁体   English

查询的结果不能被枚举多次(C#)(实体框架)(存储过程)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM