views:

4310

answers:

7

Can a LINQ enabled app run on a machine that only has the .NET 2.0 runtime installed?

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.

How can I write LINQ without using the .NET 3.5 libraries? Will it run on .NET 2.0?

A: 

No, because while you thought LINQ is really just syntactic sugar, it actually heavily used expression trees -- a feature absent in .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".

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 :)

Jon Limjap
.Net 3.0 or higher won't install on windows 2000.
Lamar
note: expression trees are used by IQueryable (as in LINQ to SQL), but not by IEnumerbale (as in LINQ to Objects)
Lucas
+2  A: 

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.

See here: http://weblogs.asp.net/fmarguerie/archive/2007/09/05/linq-support-on-net-2-0.aspx

  1. Create a new console application
  2. Keep only System and System.Core as referenced assemblies
  3. Set Copy Local to true for System.Core, because it does not exist in .NET 2.0
  4. Use a LINQ query in the Main method. For example the one below.
  5. Build
  6. Copy all the bin output to a machine where only .NET 2.0 is installed
  7. Run

(Requires .net 2.0 SP1 and I have no idea if bundling the System.Core.dll violates the EULA)

Michael Stum
redistributing System.Core.dll is a violation of Microsoft's license
Lucas
+7  A: 

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.0 which is similar to what you are wanting to do.

John Downey
A: 

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 here but it didn't convinced me either.

jdecuyper
framework 3.5, actually, not 3.0
Lucas
+2  A: 

I'm not sure about 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.

You will, however, have to implement some of the LINQ methods your self.

LINQ uses a syntatic transformation to translate queries into executable code. Basically, it will take code like this:

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).

The default IEnumerable extension methods are defined in System.Linq.Enumerable and look like this:

<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:

 <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).

You could probably create your own version of LINQ to objects in about a day or so. It's all pretty straight forward.

If you want to use DLINQ, then things would be a little bit more difficult.

Scott Wisniewski
+1, however note that expression trees (for IQueryable) are only available in 3.5+
Lucas
.. and yes, everything you said about VB 9.0 applies to C# 3.0 (using new compiler but targetting older fx version)
Lucas
But you can write your own expression tree implementation if you like.
Scott Wisniewski
+15  A: 

It's weird that no one has mentioned LINQBridge. This little awesome project is a backport of LINQ and its dependencies (Func, Action, etc) to .NET 2.0. And:

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.

Mauricio Scheffer
+1 I use this a lot. Note: this is an implementation of LINQ to Objects (the IEnumerable extensions), works perfectly if using VS2008 (C# 3.0) targeting framework 2.0+. It is NOT an implementation of LINQ to SQL or other LINQ providers.
Lucas
@Mauricio Scheffer : Now thats interesting stuff. +1
Mahin
+2  A: 

Short answer:

  • LINQ to Objects: yes (IEnumerable<T>)
  • LINQ to SQL/Entities: no (IQueryable<T>)
  • LINQ to XML/DataSets: not yet?


See this question about .Net 3.5 features available automatically or with little effort when targetting .Net 2.0 from VS2008.

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.

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 mausch mentioned). 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 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.

Lucas