.net 使用 LINQ 所需的导入和引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4093666/
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-03 14:53:43  来源:igfitidea点击:

Imports and references required to use LINQ

.netvb.netlinq

提问by JosephStyons

I have never used LINQ before, and I am getting an error in an application that does use it. This is a VB.NET (.NET 2.0) project in Visual Studio 2008.

我以前从未使用过 LINQ,并且在使用它的应用程序中出现错误。这是 Visual Studio 2008 中的 VB.NET (.NET 2.0) 项目。

Here is the offending code:

这是违规代码:

Dim orderedRows = From r In resultRows Order By r.FIELDNAME Select r

And here is the error (names changed to something generic, but otherwise accurate):

这是错误(名称更改为通用名称,但在其他方面是准确的):

Expression of type '1-dimensional array of 
Company.OurLibrary.FunctionalArea.Library.StoredProcStuff.USP_MYPROC.ResultRow'
is not queryable. Make sure you are not missing an assembly
reference and/or namespace import for the LINQ provider.
C:\project\filename.vb

So I recognize that I need to import LINQ libraries. This linkled me to add "Imports System.Linq" to the file, but that is an unresolved reference. Based on the same link, I figured I needed to add a reference to "System.Core" to my project, but it is not listed as an available option when I try to add a reference (nor is it already checked).

所以我意识到我需要导入 LINQ 库。 这个链接让我在文件中添加了“Imports System.Linq”,但这是一个未解决的参考。基于相同的链接,我认为我需要在我的项目中添加对“System.Core”的引用,但是当我尝试添加引用时它没有被列为可用选项(也没有被选中)。

I feel sure I'm missing something basic. Can someone point me in the right direction?

我觉得我错过了一些基本的东西。有人可以指出我正确的方向吗?

TL;DR: What do I need for LINQ to work?

TL;DR:我需要什么才能让 LINQ 工作?

采纳答案by Ahmad Mageed

Update: based on the comments targeting .NET 2.0 and attempting to use System.Linq would result in a compiler error as follows:

更新:基于针对 .NET 2.0 的评论并尝试使用 System.Linq 将导致编译器错误,如下所示:

Namespace or type specified in the Imports 'System.Linq' doesn't contain any public member or cannot be found.

导入“System.Linq”中指定的命名空间或类型不包含任何公共成员或无法找到。

To change the targeted framework version go to the project's Properties -> Compile -> Advanced Compile Options... set the Target Framework to ".NET Framework 3.5" and recompile.

要更改目标框架版本,请转到项目的属性 -> 编译 -> 高级编译选项...将目标框架设置为“.NET Framework 3.5”并重新编译。

In case using .NET 3.5 is not feasible then you can use LINQBridgeto make use of LINQ to Objects while targeting the .NET 2.0 framework.

如果使用 .NET 3.5 不可行,那么您可以使用LINQBridge在面向 .NET 2.0 框架的同时利用 LINQ to Objects。