EasyLinq for Delphi XE

Often you need to sort or filter an objectlist by its content or something similar. In C#, or Delphi Prism you have the powerful Linq Library which will do the job for you. Sadly, but nothing like this exists in Delphi. You need to do all by your own. Because I needed such a feature I had created some time ago a class that will supports basic Linq commands.

If you are similar with SQL-Commands you will imagine how you can benefit from this library. You will be able to execute SQL-Like-Commands directly on the objects. Of course, when using a huge amount of objects a database will still the best choice. I am currently switching to Delphi XE3 so the library will be soon available for the latest Delphi release.

Following commands are supported:

  • SELECT (optional)
  • TOP(n) limits the list of the first by n given entries.
  • DISTINCT field groups a list
  • CALC(x) calculates expressions, for example: Field1=Field2 * 5 + ( Field3 / 2 ) )
  • WHERE (x) Filter (<,>,=,<=,>=, additional Upper, Lower, Like (% Wildcard), combine with OR; AND; XOR)
  • ORDER BY Field1, Field2,… sort order (incl. ASC/DESC)
  • GROUP BY Field1, Field2 group a list (as distinct)
  • UPDATE SET (Field1=Value1, Field2=Value2) writes the values into the fields

 

How to use:

ClassLinq := TLinq.Create;
{...}
TempLinq := ClassLinq.Execute( 'ORDER BY LastName, FirstName' );
for item in TempLinq do Memo1.Lines.Add( item.ToString );
TempLinq.Free;
{...}
ClassLinq.Free

SQL Samples from the demo:

SELECT WHERE (City="Vienna")
SELECT ORDER BY City,Value
SELECT ORDER BY City,Value DESC
SELECT TOP(3) WHERE (UPPER(Lastname) like "%A%") ORDER BY MySubText.Text
SELECT CALC(value=(value*32+4)/2) ORDER BY value
SELECT TOP(3) WHERE (LOWER(Firstname)="kurt") OR (LOWER(Firstname)="edmund")
SELECT GROUP BY City ORDER BY LastName
SELECT WHERE (MySubText.Text="Sub3") OR (FirstName="Kurt")
UPDATE SET (Firstname="Max", Lastname="Mustermann") WHERE (City="Linz") OR (MySubText.Text="Sub4")
UPDATE SET (Value=CALC(23*2-4)) WHERE (City="Vienna")

Supports Delphi 2010 & XE
newer Delphi releases will follow during the next days…

Release 1.0 is outdated. Click here do download the current release.

 

One thought on “EasyLinq for Delphi XE

Leave a Reply