wpf '查询结果不能被枚举多次'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15340762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:00:37  来源:igfitidea点击:

'The Query Results cannot be enumerated more than once'

c#wpflinq

提问by KeyboardFriendly

I have a sql server procedure I am accessing with Linq to Sql. When I execute the query I am getting the error 'The Query Results cannot be enumerated more than once'. The parameter is entered in the txtName text box and the results are displayed in the lstName listview.

我有一个使用 Linq to Sql 访问的 sql 服务器过程。当我执行查询时,我收到错误“查询结果不能被枚举多次”。在txtName 文本框中输入参数,结果显示在lstName 列表视图中。

 public void GetSearchString()
 {
     Data.Database.FRCDatabaseDatacontext context = 
         new Data.Database.FRCDatabaseDatacontext();
     var result = context.GetSearchProcedure(txtName.Text);
     foreach (GetSearchProcedureResult search in result)
         if ( search.UserGuid == 
               Workspace.Instance.ActiveUser.CurrentUserActiveDirectoryGuid)
         {
             lstName.ItemsSource = result.ToList();
         }
 }

This method will return every result, but I want to return the results where the guids match.

此方法将返回每个结果,但我想返回 guids 匹配的结果。

Thanks!

谢谢!

Data.Database.FRCDatabaseDatacontext context = 
    new Data.Database.FRCDatabaseDatacontext();
var result = context.GetSearchProcedure(txtName.Text);
lstName.ItemsSource = result.ToList();

回答by Alistair Findlay

You are trying to enumerate the data more than once - once in the foreachand then again with the .ToList()for each matching UserGuid, which is causing the error.

您正在尝试多次枚举数据 - 一次在 中foreach,然后在.ToList()每个匹配中再次枚举UserGuid,这会导致错误。

Perhaps this LINQ select statement will help:

也许这个 LINQ select 语句会有所帮助:

public void GetSearchString()
{
    Data.Database.FRCDatabaseDatacontext context = new Data.Database.FRCDatabaseDatacontext();

    lstName.ItemsSource = (from s in context.GetSearchProcedure(txtName.Text) 
                  where s.UserGuid == Workspace.Instance.ActiveUser.CurrentUserActiveDirectoryGuid
                  select s).ToList();
}

回答by Vladmir

public void GetSearchString()
  {
      var context = new Data.Database.FRCDatabaseDatacontext();
      var result = context.GetSearchProcedure(txtName.Text);
      var itemSource = result.ToList();

        foreach (GetSearchProcedureResult search in itemSource)
        if (search.UserGuid == Workspace.Instance.ActiveUser.CurrentUserActiveDirectoryGuid)
        {
            lstName.ItemsSource = itemSource;
        }
    }

Anyway, It will be better to pass the parameter to procedure instead recalculating it in the code

无论如何,最好将参数传递给程序而不是在代码中重新计算它

回答by spender

Calling .ToListon an IQueryable causes it to be materialized (i.e. the query is passed on to the back-end provider). Obviously, this is something that you'd probably prefer to only do once, as it's likely to be an expensive operation. This is what the error is trying to tell you.

调用.ToListIQueryable 会导致它被物化(即查询被传递到后端提供者)。显然,您可能更愿意只执行一次,因为这可能是一项昂贵的操作。这就是错误试图告诉你的。

If you only call .ToListonce and store the result, the problem shouldgo away.

如果您只调用.ToList一次并存储结果,问题应该会消失。