During the last 10 year languages, frameworks and development platforms became better and better, we know many of this improvement, but in my opinion LINQ is one of the biggest innovations in the last 10 years.
LINQ is a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends all .NET Language like Delphi Prism with native language syntax for queries and provides class libraries to take advantage of these capabilities
There are many ways to use LINQ, querying objects, XML and database, there are other implementations which allow you to query other artifacts.
To start to understanding LINQ, I will exemplify the use of LINQ to Objects querying a list of objects.
The source code used in this post is part of the Delphi Prism demos, folder Linq under Delphi Prism demos. I developed these samples and later we added to the product.
The following example shows a simple query over an Array of String.
type Words = public class private words : Array of String := ['hello', 'Delphi Prism', 'wonderful', 'linq', 'beautiful', 'world' ]; public method SimpleSelect; method ComplexSelect; end;
The following code shows the implementation for the methos SimpleSelect using LINQ to return a list of words whose length is less than or equal to five characters.
implementation method Words.SimpleSelect; begin var shortwords := From word in words Where word.Length <= 5 Select word; Console.WriteLine('Simple select statment'); Console.WriteLine; for each word in shortwords do begin Console.WriteLine(word); end; end;
Let’s step by step on this code.
var shortwords := From word in words Where word.Length <= 5 Select word; end;
Using a simple for..each syntax you will be able to interact through the collection shortwords collection, see the code below.
for each word in shortwords do begin Console.WriteLine(word); end;
So far you ask about the capability to order, group by, etc. Simples answer, YES you can. The following example return the words grouped by length, ordered descending and returning a class and not more a string.
var groups := From word in words Order by word asc Group word by word.Length into lengthGroups Order by lengthGroups.Key desc select new class (Length:=lengthGroups.Key,Words:=lengthGroups);
We add the clause Order by and Group, where Key represents the string Length for each collection element.
One of the most powerful feature is the ability to create dynamic classes using the syntax new class, beyond that we are using the lambda expression.
Lambda expressions are a special type of expression that can be best thought of as mapping a set of one or more values to a result, using a function, sample below Length:=lengthGroups.Key .
select new class (Length:=lengthGroups.Key,Words:=lengthGroups);
In case you need to interact through the group, just loop the variable groups, where the property Words represent the list of words for each group.
for each grupo in groups do begin Console.WriteLine('Words of length ' + grupo.Length); for each word in grupo.Words do Console.WriteLine(' ' + word); end;
Next post I will explain LINQ to XML, see you soon.