LINQ中的foreach结果不起作用 [英] foreach in linq result not working

查看:66
本文介绍了LINQ中的foreach结果不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道这里出了什么问题,当我运行应用程序时,它说不支持指定的方法",它指向foreach循环中的查询中的var结果".请帮忙...

Don't know what's wrong here, when I run the application it says "Specified method is not supported" pointing at "var result in query" in foreach loop. Please help...

var query = from c in entities.Customer
            select c.CustomerName;

List<string> customerNames = new List<string>();

foreach (var result in query)
{
    customerNames.Add(result.ToString());  
}

使用ToList()也会产生相同的错误.

using ToList() also gives the same error.

推荐答案

发生错误的原因是范围,这是不支持的方法"错误告诉您的.

The reason for your error is scope, which is what the "method not supported" error is telling you.

通常在使用Linq来[填写空白] ORM时发生.因此,我猜您的实体必须来自ORM工具,例如Entity Framework,而您使用的是Linq to Entities.

This usually happens when using a Linq to [fill in the blank] ORM. So, I'm guessing your entities must be from an ORM tool, something like Entity Framework, and you are using something like Linq to Entities.

使用linq时,直到您访问它时,查询才会被列举出来,对于ORM而言,这意味着访问数据库或其他数据存储库.如果您不知道该延迟动作,可能会导致某些奇怪的行为,例如此错误.

When using linq your query is not enumerated out until you access it, which for an ORM means hitting the database or other data repository. This delayed action can cause some strange behavior if you do not know it is there, such as this error.

但是,您有本地(非linq)代码,并且您的查询是交织在一起的,因此linq to []编译器在编译linq代码时不知道如何处理本地代码.因此,出现不支持的方法"错误-与从类外部引用私有方法基本相同,您调用的方法在当前作用域中是未知的.

But, you have local (non-linq) code and your query intertwined, so the linq to [] compiler does not know how to handle your local code when compiling the linq code. Thus the "method not supported" error - it is basically the same as referencing a private method from outside of the class, the method you called is unknown in the current scope.

换句话说,当您执行result.ToString()时,编译器将尝试编译查询并访问数据库,但对CustomerNames的私有变量或foreach方法一无所知.数据库逻辑和本地对象逻辑必须分开放置-在本地使用之前完全解决数据库查询结果.

In other words the compiler is trying to compile your query and hit the database when you do the result.ToString(), but does not know anything about the private variable of CustomerNames or the foreach method. The database logic and the local object logic have to be kept separate - completely resolve the database query results before using locally.

您应该可以这样写:

var customerNames  = entities.Customer.Select(c => c.CustomerName).ToList();

如果您必须保留foreach(对于更复杂的逻辑,而不是为了这个简单的示例),您仍然需要在涉及任何非"n"之前解决Linq to []部分(通过强制其枚举查询结果). -linq代码:

If you have to keep the foreach (for more complicated logic, not for this simple of an example) you still need to resolve the Linq to [] portion (by forcing it to enumerate the query results) prior to involving any non-linq code:

var query = from c in entities.Customer
            select c.CustomerName;

var qryList = query.ToList();

List<string> customerNames = new List<string>();

foreach (var result in qryList)
{
    customerNames.Add(result.ToString());  
}

这篇关于LINQ中的foreach结果不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆