.NET 2.0 运行时上的 LINQ
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2138/
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
LINQ on the .NET 2.0 Runtime
提问by urini
Can a LINQ enabled app run on a machine that only has the .NET 2.0 runtime installed?
启用 LINQ 的应用程序能否在仅安装了 .NET 2.0 运行时的机器上运行?
In theory, LINQ is nothing more than syntactic sugar, and the resulting IL code should look the same as it would have in .NET 2.0.
理论上,LINQ 只不过是语法糖,生成的 IL 代码应该与 .NET 2.0 中的代码看起来一样。
How can I write LINQ without using the .NET 3.5 libraries? Will it run on .NET 2.0?
如何在不使用 .NET 3.5 库的情况下编写 LINQ?它会在 .NET 2.0 上运行吗?
采纳答案by Michael Stum
There are some "Hacks" that involve using a System.Core.dll from the 3.5 Framework to make it run with .net 2.0, but personally I would not want use such a somewhat shaky foundation.
有一些“技巧”涉及使用 3.5 框架中的 System.Core.dll 使其与 .net 2.0 一起运行,但我个人不希望使用这种有点不稳定的基础。
See here: LINQ support on .NET 2.0
请参见此处:.NET 2.0 上的 LINQ 支持
- Create a new console application
- Keep only System and System.Core as referenced assemblies
- Set Copy Local to true for System.Core, because it does not exist in .NET 2.0
- Use a LINQ query in the Main method. For example the one below.
- Build
- Copy all the bin output to a machine where only .NET 2.0 is installed
- Run
- 创建一个新的控制台应用程序
- 仅保留 System 和 System.Core 作为引用程序集
- 将 System.Core 的 Copy Local 设置为 true,因为它在 .NET 2.0 中不存在
- 在 Main 方法中使用 LINQ 查询。例如下面的那个。
- 建造
- 将所有 bin 输出复制到仅安装了 .NET 2.0 的机器上
- 跑
(Requires .net 2.0 SP1 and I have no idea if bundling the System.Core.dll violates the EULA)
(需要 .net 2.0 SP1,我不知道捆绑 System.Core.dll 是否违反 EULA)
回答by Mauricio Scheffer
It's weird that no one has mentioned LINQBridge. This little awesome project is a backport of LINQ (IEnumerable, but without IQueryable) and its dependencies (Func, Action, etc) to .NET 2.0. And:
奇怪的是没有人提到LINQBridge。这个很棒的小项目是 LINQ(IEnumerable,但没有 IQueryable)及其依赖项(Func、Action 等)到 .NET 2.0 的反向移植。和:
If your project references LINQBridge during compilation, then it will bind to LINQBridge's query operators; if it references System.Core during compilation, then it will bind to Framework 3.5's query operators.
如果您的项目在编译期间引用了 LINQBridge,那么它将绑定到 LINQBridge 的查询运算符;如果它在编译期间引用 System.Core,那么它将绑定到 Framework 3.5 的查询运算符。
回答by John Downey
In theory yes, provided you distribute the LINQ specific assemblies and any dependencies. However that is in violation of Microsoft's licensing. Scott Hanselman wrote a blog post about Deploying ASP.NET MVC on ASP.NET 2.0which is similar to what you are wanting to do.
理论上是的,前提是您分发特定于 LINQ 的程序集和任何依赖项。然而,这违反了微软的许可。Scott Hanselman 写了一篇关于在 ASP.NET 2.0 上部署 ASP.NET MVC的博客文章,这与您想要做的类似。
回答by Stefan Steiger
You can use the LINQ sources from mono (.NET for Linux) to get LINQ running on .NET 2.0.
您可以使用来自 mono(用于 Linux 的 .NET)的 LINQ 源来让 LINQ 在 .NET 2.0 上运行。
IEnumerable<T> : yes
IQueryable<T> : yes
LINQ to XML : has been working in the trunk, but due to further additions, the trunk doesn't compile anymore
Someone has done it here:
LINQ for .NET 2.0
有人在这里做过:
LINQ for .NET 2.0
回答by Lucas
Short answer:
简短的回答:
- LINQ to Objects: yes(
IEnumerable<T>) - LINQ to SQL/Entities: no(
IQueryable<T>) - LINQ to XML/DataSets: not yet?
- LINQ to Objects: 是(
IEnumerable<T>) - LINQ to SQL/实体:否(
IQueryable<T>) - LINQ to XML/DataSets:还没有?
See this questionabout .Net 3.5 features available automatically or with little effort when targetting .Net 2.0 from VS2008.
当从 VS2008 定位 .Net 2.0 时,请参阅有关 .Net 3.5 功能自动或轻松可用的问题。
Basically, anything that is only "syntax sugar" and the new compilers (C# 3.0, VB 9.0) emit as 2.0-compatible IL will work. This includes many features used by LINQ such as anonymous classes, lambdas as anonymous delegates, automatic properties, object initializers, and collection initializers.
基本上,任何只是“语法糖”和新编译器(C# 3.0、VB 9.0)作为 2.0 兼容 IL 发出的东西都可以工作。这包括 LINQ 使用的许多功能,例如匿名类、作为匿名委托的 lambda、自动属性、对象初始值设定项和集合初始值设定项。
Some LINQ features use classes, interfaces, delegates, and extension methods that live in the new 3.5 assemblies (such as System.Core.dll). Redistributing these assemblies is a license violation, but they could be reimplemented. Using extension methods need only that you declare an empty System.Runtime.CompilerServices.ExtensionAttribute. LINQ to Objects relies on IEnumerable<T>extensions and several delegate declarations (the Action<T>and Func<T>families) and have been implemented in LINQBridge(as mauschmentioned). LINQ to XML and LINQ to DataSets rely on LINQ to Objects which I guess could also be implemented for .Net 2.0, but I haven't seen this done yet.
某些 LINQ 功能使用新 3.5 程序集中的类、接口、委托和扩展方法(例如 System.Core.dll)。重新分发这些程序集是违反许可证的,但它们可以重新实现。使用扩展方法只需要声明一个空的System.Runtime.CompilerServices.ExtensionAttribute. LINQ to Objects 依赖于IEnumerable<T>扩展和几个委托声明(theAction<T>和familyFunc<T>),并且已经在LINQBridge 中实现(如mausch所述)。LINQ to XML 和 LINQ to DataSets 依赖于 LINQ to Objects,我想这也可以为 .Net 2.0 实现,但我还没有看到这样做。
LINQ to SQL and LINQ to Entities require many new classes (DataContext/ObjectContext, lots of attributes, EntitySet<T>, EntityRef<T>, Link<T>, IQueryable<T>, etc) and expression trees, which, even if somehow reimplemented, will probably require at least .Net 2.0 SP1 to work.
LINQ to SQL和LINQ到实体需要许多新的类(DataContext/ ObjectContext,属性的地段EntitySet<T>,EntityRef<T>,Link<T>,IQueryable<T>,等)和表达式树,其中,即使在某种程度上重新实现,可能需要至少NET 2.0 SP1工作。
回答by Scott Wisniewski
I'm not sure about C#.
我不确定 C#。
I do know, however, that you can write VB LINNQ code w/out the 3.5 libraries as long as you use the VS 2008 compiler to target the 2.0 framework.
但是,我确实知道,只要您使用 VS 2008 编译器以 2.0 框架为目标,您就可以编写不带 3.5 库的 VB LINNQ 代码。
You will, however, have to implement some of the LINQ methods your self.
但是,您必须自己实现一些 LINQ 方法。
LINQ uses a syntatic transformation to translate queries into executable code. Basically, it will take code like this:
LINQ 使用语法转换将查询转换为可执行代码。基本上,它将采用如下代码:
dim q = from x in xs where x > 2 select x*4;
and convert it into code like this:
并将其转换为如下代码:
dim q = xs.where(function(x) x > 2).select(function(x) x * 4);
For the LINQ functionality that ships with the 3.5 framework, those methods are implemented as extension methods on either IEnumerable or IQueryable (there's also a bunch of methods that work on data sets too).
对于 3.5 框架附带的 LINQ 功能,这些方法是作为 IEnumerable 或 IQueryable 上的扩展方法实现的(还有一堆方法也适用于数据集)。
The default IEnumerable extension methods are defined in System.Linq.Enumerable and look like this:
默认的 IEnumerable 扩展方法在 System.Linq.Enumerable 中定义,如下所示:
<Extension()>
public function Select(of T, R)(source as IEnumerable(of T), transform as Func(of T, R)) as IEnumerable(of R)
'do the transformation...
end function
The IQueryable extension methods take expressions trees as arguments, rather than lambdas. They look like this:
IQueryable 扩展方法将表达式树作为参数,而不是 lambda。它们看起来像这样:
<Extension()>
public function Select(of T, R)(source as IQueryable<T>, transform as Expression(of Func(of T, R))
'build a composite IQueryable that contains the expression tree for the transformation
end function
The expression tree versions enable you to get a tree representation of the expressions provided to the clauses which can then be used to generate SQL code (or what ever else you want).
表达式树版本使您能够获得提供给子句的表达式的树表示,然后可将其用于生成 SQL 代码(或您想要的任何其他代码)。
You could probably create your own version of LINQ to objects in about a day or so. It's all pretty straight forward.
您可能会在大约一天左右的时间内创建自己的 LINQ to objects 版本。这一切都非常简单。
If you want to use DLINQ, then things would be a little bit more difficult.
如果你想使用 LINQ,那么事情会有点困难。
回答by Jon Limjap
No, because while you thought LINQ is really just syntactic sugar, it actually heavily used expression trees -- a feature absent in .NET 2.0.
不,因为虽然您认为 LINQ 实际上只是语法糖,但它实际上大量使用了表达式树 - .NET 2.0 中没有的功能。
That being said .NET 3.5 only builds up on top of .NET 2.0, and that's the reason why the IL doesn't look "different" or "special".
话虽如此,.NET 3.5 仅建立在 .NET 2.0 之上,这就是 IL 看起来并不“不同”或“特殊”的原因。
I do not see a reason why you shouldn't just install the .NET 3.5 Framework. Everything .NET 2.0 will work fine on it, promise :)
我看不出为什么不应该只安装 .NET 3.5 Framework。.NET 2.0 的一切都可以正常工作,承诺:)
回答by jdecuyper
As far as I know the LINQ library is only available since the framework 3.0. If you want to use something similar in the framework 2.0, you would need to rewritte it yourself :) or find a similar third-party library. I only found a bit of information herebut it didn't convinced me either.
据我所知,LINQ 库仅在框架 3.0 之后可用。如果你想在框架 2.0 中使用类似的东西,你需要自己重写:) 或找到一个类似的第三方库。我只在这里找到了一些信息,但它也没有说服我。

