views:

869

answers:

2

Is it possible to use LINQ in win32 DELPHI applications

+2  A: 

Yes and No. LINQ can really be thought of as two different items.

The first is the SQL like query syntax. It's what allows you to write the following in C#.

var query = from it in "foobar" select Char.ToUpper(it);

For delphi to use this version of LINQ, it would need to add explicit syntax support. AFAIK this does not exist.

Under the hood though, all LINQ queries are translated into a set of query expressions. These typically involve a heavy use of lambda expressions and closures. The above code is equivalent to the following non-SQL syntax version.

var query = "foobar".Select(x => Char.ToUpper(x));

I don't know the level of lambda or delegate support in Delphi but it should be possible to access LINQ in this method from Delphi.

JaredPar
- you have a small error, you need to replace "it" with "x".Delphi Win32 lacks almost every feauture that makes the second example work. There is no VAR. string and char are not objects, and you don't have the short syntax lamda expressions.So if you can do it, it would be a lot uglier.
mliesen
@mliesen, thanks fixed the typo.
JaredPar
+13  A: 

Delphi 2009 has generics, class helpers and anonymous method support, but not lambda, extension methods or type inference. Lambda expressions are probably coming in a future version of Delphi, but they are not on the official road map yet (a new one should be coming soon hopefully). Also Delphi for Win32 does not have access to all the LINQ libraries.

So the short answer is NO, you can't do LINQ in Win32 Delphi. You can how ever do some similar things, and you can technically even access LINQ through COM (as you can with any .NET classes), but it would kind of defeat the point without the cool LINQ syntax.

LINQ is really a .NET technology. While Delphi will most likely develop the language features that make LINQ possible, the underlying .NET libraries are for .NET development only.

I would suggest using RemObject Data Abstract or similar.

Jim McKeeth
Umm... aren't extension methods and class helpers two names for the exact same thing? Only difference is one of them's used in Delphi and the other's used in C#, or is there some critical bit of functionality I'm overlooking?
Mason Wheeler
Class Helpers are only applied to one class. Extension Methods are a little more broad. They are very similar, and you could probably do it with Class Helpers, but it would require more code.
Jim McKeeth