Jump to content
Epo

Spring4D GroupBy Second question

Recommended Posts

Using the GroupBy method, I wonder if it possible to "imbricate" GroupBy in Spring4D to generate a "two levels" tree in one pass
Some sort of:

var
 GByData    : IEnumerable<IGrouping<string,TVariable>>;
 GByData2   : IEnumerable<IGrouping<string,TVariable>>;
begin
  GByData := TEnumerable.GroupBy<TVariable, string>(IEnumTV,
    function (const ASVar: TVariable): string
    begin
      Result :=  ASVar.AppName;
    end);

  GByData2 := TEnumerable.GroupBy<TVariable, string>(GByData,
    function (const ASVar: TVariable): string
    begin
      Result :=  ASVar.PrjName;
    end);
end;


Thanks a lot,

 

Eddy

 

Share this post


Link to post

Yes but beware this will be very nasty - thanks to poor type inference in Delphi.

You have to use the GroupBy overload with 4 generic parameters:

 

type
  TGroup = record
    AppName: string;
    Groups: IEnumerable<IGrouping<string,TVariable>>;
  end;
var
  GroupedTwoLevels: IEnumerable<TGroup>;
begin
  GroupedTwoLevels := TEnumerable.GroupBy<TVariable, string, TVariable, TGroup>(IEnumTV,
    function (const v: TVariable): string
    begin
      Result := v.AppName;
    end,
    function (const v: TVariable): TVariable
    begin
      Result := v;
    end,
    function (const key: string; const vars: IEnumerable<TVariable>): TGroup
    begin
      Result.AppName := key;
      Result.Groups := TEnumerable.GroupBy<TVariable, string>(vars,
        function(const v: TVariable): string
        begin
          Result := v.PrjName;
        end);
    end);

  ListBox5.Clear;

  for var g1 in GroupedTwoLevels do
  begin
    ListBox5.Items.Add(g1.AppName);
    for var g2 in g1.Groups do
    begin
      ListBox5.Items.Add('  ' + g2.Key);
      for var v in g2 do
        ListBox5.Items.Add('    ' + v.AppName + '_' + v.PrjName);
    end;
  end;

 

 

Share this post


Link to post

Thanks for this example. Precisely, I was wondering how to use this version of GroupBy.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×