Skip to toolbar

LINQ has been with kbmMW for quite a while now, and it has slowly evolved with each release.

Next release of kbmMW will contain features to make it easy to LINQ plain strings, integer, int64 and floating point values.

This post is giving a taste of how it works.

Sample 1

Let us work on an array of integer values, sort them descending and pick the top 5 of them:

var
   i,j:integer;
   aInt,aInt2:TArray<integer>;
begin
     // Setup an array to work on. Depending on Delphi version, 
     // it can be instantiated in various ways.
     aInt:=TArray<integer>.Create(10,88,30,50,99,100,55,200);

     // Using the array, lets sort it descending, pick the top 5 and return those
     // as an array of integers
     aInt2:=Linq.Using(TValue.From(aInt))
                .Sort('value:D')
                .First(5)
                .AsIntegerArray;

     // Print the result
     for i:=0 to high(aInt2) do
         Memo1.Lines.Add(inttostr(aInt2[i]));
end;

Sample 2

Let us build on the above sample, but this time we want to split the array into sub arrays:

var
   i,j:integer;
   aInt:TArray<integer>;
   aaInt:TArray<TArray<integer>>;
begin
     // Setup an array to work on. Depending on Delphi version, 
     // it can be instantiated in various ways. 
     aInt:=TArray<integer>.Create(10,88,30,50,99,100,55,200);     

     // Using the above array, lets sort it ascending, split it into
     // groups of 3 integer values, and return an array of an array of integers.
     aaInt:=Linq.Using(TValue.From(aInt))
                .Sort('value')
                .Split(3)
                .AsIntegerArray;

     // Print out the result
     for i:=Low(aaInt) to High(aaInt) do
     begin
          Memo1.Lines.Add(' -- '+inttostr(i)+' --');
          for j:=Low(aaInt[i]) to High(aaInt[i]) do
              Memo1.Lines.Add(inttostr(aaInt[i][j]));
     end;
end;

Further LINQ now also supports Int64 values directly.

When first data has been entered using one of the many Using variations, you can use all LINQ’s methods to search, order, group, aggregate etc. the data, after which you can output the result as datasets, arrays, lists, native value and many other types.

If you like kbmMW, please let others know about it!

 9,202 total views,  1 views today

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.