Epo 1 Posted March 29, 2021 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
Stefan Glienke 2002 Posted March 29, 2021 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
Epo 1 Posted March 29, 2021 Thanks for this example. Precisely, I was wondering how to use this version of GroupBy. Share this post Link to post