Wednesday, July 29, 2009

LINQ inside X++ ?

LINQ (Language INtegrated Query) in C# is simply a work of art. 
It's a good example of "Less is more".
With a few lines of code you can express an otherwise complex operation.


In X++ something similar can be done with queries in sql tables, but not in an Array og List object.
As a hobby project, I created a small Domain Specific Language called "AxLINQ", that can make this possible.


Example (C#)
List<string> names = new List<string>();

var query = from name in names
order by name
select "Name: " + name;

Example (X++)
str name;
Array     names = new Array(Types::String);
AxLINQ_IQueryable axQuery = xfrom(name).in(names)
.orderBy(name)
.select("Name: " + name);

The axQuery object can give you an enumerator, to enumerate the ordered list of names.


AxLINQ implements the standard operators of SELECT, WHERE, ORDER BY, GROUP BY, JOIN and several other functions like DISTINCT, ANY, COUNT, MAX, MIN etc.

Download and try for yourself

If you wish to try it out, you can download the framework at http://axlinq.codeplex.com