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?
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:
and convert it into code like this:
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:
The IQueryable extension methods take expressions trees as arguments, rather than lambdas. They look like this:
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.
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: LINQ support on .NET 2.0
(Requires .net 2.0 SP1 and I have no idea if bundling the System.Core.dll violates the EULA)
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 :)
Short answer:
IEnumerable<T>
)IQueryable<T>
)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 onIEnumerable<T>
extensions and several delegate declarations (theAction<T>
andFunc<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.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:
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.