Group method (with objects)
Description
The Group method groups the elements of a sequence accoring to a secified key. Every element of thre result contains a property Key for the group value and a list of the elements of the group
Sample
This sample goups the orders using the aricle as key.
var data := List<OrderItem>{}{ ;
OrderItem{}{ArticleCode := "Apple", Quantity := 10, Price := 1.5m}, ;
OrderItem{}{ArticleCode := "Banana", Quantity := 2, Price := 2m}, ;
OrderItem{}{ArticleCode := "Apple", Quantity := 5, Price := 1.5m} ;
}
var result := data:GroupBy({ q => q:ArticleCode })
foreach var groupItem in result
Console.WriteLine(groupItem:key)
foreach var item in groupItem
Console.WriteLine(String.Format("{0} - {1} - {2}", item:ArticleCode, item:Quantity, item:Price))
next
next
Output
Apple
Apple - 10 - 1,5
Apple - 5 - 1,5
Banana
Banana - 2 - 2
Complete sample
using System
using System.Linq
using System.Collections.Generic
procedure Execute() as void strict
var data := List<OrderItem>{}{ ;
OrderItem{}{ArticleCode := "Apple", Quantity := 10, Price := 1.5m}, ;
OrderItem{}{ArticleCode := "Banana", Quantity := 2, Price := 2m}, ;
OrderItem{}{ArticleCode := "Apple", Quantity := 5, Price := 1.5m} ;
}
var result := data:GroupBy({ q => q:ArticleCode })
foreach var groupItem in result
Console.WriteLine(groupItem:key)
foreach var item in groupItem
Console.WriteLine(String.Format("{0} - {1} - {2}", item:ArticleCode, item:Quantity, item:Price))
next
next
return
class OrderItem
public property ArticleCode as string auto
public property Quantity as int auto
public property Price as decimal auto
end class
var data := List<OrderItem>{}{ ;
OrderItem{}{ArticleCode := "Apple", Quantity := 10, Price := 1.5m}, ;
OrderItem{}{ArticleCode := "Banana", Quantity := 2, Price := 2m}, ;
OrderItem{}{ArticleCode := "Apple", Quantity := 5, Price := 1.5m} ;
}
var result := from q in data ;
group q by q:ArticleCode into groups ;
select groups
foreach var groupItem in result
Console.WriteLine(groupItem:key)
foreach var item in groupItem
Console.WriteLine(String.Format("{0} - {1} - {2}", item:ArticleCode, item:Quantity, item:Price))
next
next
Output
Apple
Apple - 10 - 1,5
Apple - 5 - 1,5
Banana
Banana - 2 - 2
Complete sample
using System
using System.Linq
using System.Collections.Generic
procedure Execute() as void strict
var data := List<OrderItem>{}{ ;
OrderItem{}{ArticleCode := "Apple", Quantity := 10, Price := 1.5m}, ;
OrderItem{}{ArticleCode := "Banana", Quantity := 2, Price := 2m}, ;
OrderItem{}{ArticleCode := "Apple", Quantity := 5, Price := 1.5m} ;
}
var result := from q in data ;
group q by q:ArticleCode into groups ;
select groups
foreach var groupItem in result
Console.WriteLine(groupItem:key)
foreach var item in groupItem
Console.WriteLine(String.Format("{0} - {1} - {2}", item:ArticleCode, item:Quantity, item:Price))
next
next
return
class OrderItem
public property ArticleCode as string auto
public property Quantity as int auto
public property Price as decimal auto
end class