Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.NET2.0运行时上的LINQ_.net_Linq_.net 3.5_.net 2.0_Clr - Fatal编程技术网

.NET2.0运行时上的LINQ

.NET2.0运行时上的LINQ,.net,linq,.net-3.5,.net-2.0,clr,.net,Linq,.net 3.5,.net 2.0,Clr,启用LINQ的应用程序能否在仅安装了.NET 2.0运行时的计算机上运行 从理论上讲,LINQ只不过是一种语法上的糖分,由此产生的IL代码应该与.NET2.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

启用LINQ的应用程序能否在仅安装了.NET 2.0运行时的计算机上运行

从理论上讲,LINQ只不过是一种语法上的糖分,由此产生的IL代码应该与.NET2.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

如何在不使用.NET3.5库的情况下编写LINQ?它会在.NET2.0上运行吗?

不会,因为虽然您认为LINQ实际上只是语法糖,但它实际上大量使用了表达式树--这是.NET2.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
也就是说.NET3.5只是建立在.NET2.0之上,这就是为什么IL看起来没有“不同”或“特殊”的原因

我看不出有什么理由不应该只安装.NET3.5框架。所有的.NET 2都会很好地工作,承诺:

< p>有一些“黑客”,包括使用来自3.5框架的Soal.C..DLL使之运行.NET 2,但我个人不希望使用这样一个有点动摇的基础。 请看这里:

  • 创建新的控制台应用程序
  • 仅保留System和System.Core作为引用程序集
  • 将System.Core的Copy Local设置为true,因为它在.NET 2.0中不存在
  • 在Main方法中使用LINQ查询。例如下面的一个
  • 建造
  • 将所有箱子输出复制到仅安装.NET 2.0的机器上

  • (需要.net 2.0 SP1,我不知道捆绑System.Core.dll是否违反EULA)

    理论上是的,前提是您分发特定于LINQ的程序集和任何依赖项。然而,这违反了微软的授权。Scott Hanselman写了一篇博客文章,内容与您想要做的类似。

    据我所知,LINQ库仅在framework 3.0之后才可用。 如果您想在Framework2.0中使用类似的东西,您需要自己重写:)或找到类似的第三方库。我只找到了一点信息,但也没有说服我。

    我不确定C

    但是,我确实知道,只要使用VS2008编译器以2.0框架为目标,就可以使用3.5库编写VB LINNQ代码

    但是,您必须自己实现一些LINQ方法

    LINQ使用语法转换将查询转换为可执行代码。基本上,需要这样的代码:

    dim q = from x in xs where x > 2 select x*4;
    
    dim q = xs.where(function(x) x > 2).select(function(x) x * 4);
    
    <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
    
     <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
    
    并将其转换为如下代码:

    dim q = from x in xs where x > 2 select x*4;
    
    dim q = xs.where(function(x) x > 2).select(function(x) x * 4);
    
    <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
    
     <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
    
    对于3.5框架附带的LINQ功能,这些方法被实现为IEnumerable或IQueryable上的扩展方法(还有一系列方法也适用于数据集)

    默认IEnumerable扩展方法在System.Linq.Enumerable中定义,如下所示:

    dim q = from x in xs where x > 2 select x*4;
    
    dim q = xs.where(function(x) x > 2).select(function(x) x * 4);
    
    <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
    
     <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
    
    
    公共函数选择(属于T,R)(源为IEnumerable(属于T),转换为Func(属于T,R))作为IEnumerable(属于R)
    “进行转换。。。
    端函数
    
    IQueryable扩展方法将表达式树作为参数,而不是lambda。它们看起来像这样:

    dim q = from x in xs where x > 2 select x*4;
    
    dim q = xs.where(function(x) x > 2).select(function(x) x * 4);
    
    <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
    
     <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
    
    
    公共函数Select(属于T,R)(源为可查询,转换为表达式(属于Func(属于T,R))
    '构建包含转换表达式树的复合IQueryable
    端函数
    
    表达式树版本使您能够获取提供给子句的表达式的树表示形式,然后可以使用这些表达式生成SQL代码(或您想要的任何其他内容)

    你大概可以在一天左右的时间内创建自己版本的LinqTo对象,这一切都很简单


    如果您想使用DLINQ,那么事情会有点困难。

    没有人提到这一点,这很奇怪。这个非常棒的小项目是LINQ(IEnumerable,但没有IQueryable)及其对.NET 2.0的依赖项(Func、Action等)的后端口。并且:

    如果您的项目引用LINQBridge 在编译期间,它将绑定 到LINQBridge的查询运算符;如果 参考系统 编译,然后它将绑定到 框架3.5的查询运算符

    简短答复:

    • LINQ到对象:是
      IEnumerable
    • LINQ到SQL/实体:否(
      IQueryable
    • LINQ到XML/数据集:还没有

    请参阅VS2008中针对.Net 2.0时自动或轻松提供的.Net 3.5功能

    基本上,任何只是“语法糖”的东西和新编译器(C#3.0、VB9.0)都可以作为2.0兼容的IL发出。这包括LINQ使用的许多特性,例如匿名类、作为匿名委托的lambda、自动属性、对象初始值设定项和集合初始值设定项

    一些LINQ特性使用新3.5程序集中的类、接口、委托和扩展方法(如System.Core.dll)。重新分发这些程序集违反了许可证,但可以重新实现。使用扩展方法只需声明空的
    System.Runtime.CompilerServices.ExtensionAttribute
    。LINQ到对象依赖于
    IEnumerable
    扩展和多个委托声明(操作
    Func
    系列)和已在中实现(如前所述)。LINQ to XML和LINQ to数据集依赖于LINQ to对象,我想这也可以在.Net 2.0中实现,但我还没有看到这样做


    LINQ到SQL和LINQ到实体需要许多新类(
    DataContext
    /
    ObjectContext
    ,许多属性,
    EntitySet
    EntityRef
    链接
    IQueryable
    ,等等)和表达式树,即使以某种方式重新实现,也可能需要至少.Net 2.0 SP1才能工作。

    您可以使用mono(.Net for Linux)的LINQ源在.Net 2.0上运行LINQ

    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
    
    IEnumerable:是
    伊奎尔:是的
    LINQ to XML:一直在主干中工作,但由于毛发