繁体   English   中英

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

[英]The Result of a Query Cannot be Enumerated More than Once (C#) (Entity Framework) (Stored Procedure)

当我到达db.GetLabelComponentsByLabelID(LabelIDs.ElementAt(i).Value.ToString()).ToList()

我收到以下异常: The Result of a Query Cannot be Enumerated More than Once

我试图通过像这个答案建议的那样调用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++)
    {
        ....
        ....

您在第一次db 调用时需要ToList ,因为您要多次枚举LabelIDs值。 LabelIDs.Count第一次运行查询,然后LabelIDs.ElementAt稍后再次运行它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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