简体   繁体   English

“查询结果不能被多次枚举”

[英]'The Query Results cannot be enumerated more than once'

I have a sql server procedure I am accessing with Linq to Sql. 我有一个用Linq访问Sql的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. 在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. 此方法将返回每个结果,但是我想返回与GUID匹配的结果。

Thanks! 谢谢!

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

You are trying to enumerate the data more than once - once in the foreach and 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();
}
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 无论如何,最好将参数传递给过程,而不要在代码中重新计算它

Calling .ToList on an IQueryable causes it to be materialized (ie the query is passed on to the back-end provider). 在IQueryable上调用.ToList会使其实现(即,查询将传递到后端提供程序)。 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. 这就是错误要告诉您的内容。

If you only call .ToList once and store the result, the problem should go away. 如果只调用一次.ToList并存储结果,则问题消除。

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

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