Using LINQ to Objetcs in Delphi Prism

Let's share the knowledge with your friends

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.

  • Variável shortwords – represent a collection of strings, because in this case we represent each element of the collection as string.
  • From word – variable that represent each element selected in the Array de String
  • in words – collection where LINQ will interact, in the database world it will be our table
  • Where word.Length <= 5 – condition, filter all words with length less or equal 5, look we are using the variable word
  • Select word – represented each element of the collection returned
  •   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.


    Let's share the knowledge with your friends
    2 replies
      • Andreano Lanusse
        Andreano Lanusse says:

        Hi Mark,

        If you have your application in Delphji 7 the natural way is Delphi XE, migrate from native to .NET is not something easy and I believe you can add more value in your system with the new implemented since Delphi XE, Delphi Prism could be a good solution to implememt web and mobile applications.

        Reply

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.