我有一個資料陣列,我需要將它們按 2 個屬性分組,然后計算每個組的第三個屬性的總和。我想通過 Linq 盡可能快地做到這一點。
到目前為止,這是我的演示代碼:
class costs {
[string] $first;
[string] $last;
[int] $price;
costs([string]$first, [string]$last, [int] $price){
$this.first = $first
$this.last = $last
$this.price = $price
}
}
[costs[]]$costs = @(
[costs]::new('peter', 'parker', 1),
[costs]::new('peter', 'parker', 2),
[costs]::new('paul', 'summer', 3),
[costs]::new('paul', 'winter', 4),
[costs]::new('mary', 'winter', 5)
)
# group by full name:
$groupBy = [Func[Object,string]] {$args[0].first $args[0].last}
$groupResult = [Linq.Enumerable]::GroupBy($costs, $groupBy)
# sum the costs per group:
$selectFunc = [Func[Object,int]] {$sum=0; foreach($p in $args[0].price){$sum = $p};$sum}
$selectResult = [Linq.Enumerable]::Select($groupResult, $selectFunc)
$selectResult
selectResult 向我顯示了每個用戶的正確成本總和。但我正在努力從初始陣列中獲得兩個用戶屬性的總和。我也不確定,是否可以將兩個 Linq 呼叫合并為一個以使其更快。這里非常歡迎任何輸入(“為什么是 Linq?”除外)。
更新
根據答案,我更新了這樣的代碼:
class costs {
[string] $first;
[string] $last;
[int] $price;
costs([string]$first, [string]$last, [int] $price){
$this.first = $first
$this.last = $last
$this.price = $price
}
}
[costs[]]$costs = @(
[costs]::new('peter', 'parker', 1),
[costs]::new('peter', 'parker', 2),
[costs]::new('paul', 'summer', 3),
[costs]::new('paul', 'winter', 4),
[costs]::new('mary', 'winter', 5)
)
foreach($doubler in 0..15){$costs = $costs}
cls
write-host "processing $($costs.count) elements."
(measure-command {
# group by full name:
$groupBy = [Func[Object,string]] {$args[0].first $args[0].last}
$groupResult = [Linq.Enumerable]::GroupBy($costs, $groupBy)
# sum the costs per group:
$selectFunc = [Func[Object,Object]]{
$sum=0
foreach($p in $args[0].price){
$sum = $p
}
foreach($a in $args[0]) {
[costs]::new($a.first, $a.last, $sum)
break
}
}
$selectResult = [Linq.Enumerable]::Select($groupResult, $selectFunc)
$result = [Linq.Enumerable]::ToArray($selectResult)
}).TotalSeconds
$result
# and for the books the same procedure with a dataTable (slower):
$table = [System.Data.DataTable]::new('table')
[void]$table.Columns.Add('first', [string])
[void]$table.Columns.Add('last', [string])
[void]$table.Columns.Add('price', [int])
$resultTable = $table.Clone()
# fill table with above test-data:
foreach($c in $costs){
$null = $table.rows.Add($c.first, $c.last, $c.price)
}
(measure-command {
$groupBy = [Func[System.Data.DataRow,string]] {$args[0].first $args[0].last}
$groupResult = [Linq.Enumerable]::GroupBy([System.Data.DataRow[]]$table.Rows, $groupBy)
# sum the costs per group:
$selectFunc = [Func[object,System.Data.DataRow]]{
$sum=0
foreach($p in $args[0].price){
$sum = $p
}
foreach($a in $args[0]) {
$resultTable.rows.Add($a.first, $a.last, $sum)
break
}
}
$selectResult = [Linq.Enumerable]::Select($groupResult, $selectFunc)
$null = [Linq.Enumerable]::ToList($selectResult)
}).TotalSeconds
$resultTable
超過 300000 個元素的運行時間約為 2.5 秒。沒那么糟。到目前為止,如果不切換到嵌入式 C# 代碼,我找不到更快的方法。
uj5u.com熱心網友回復:
將$selectFunc定義更改為 return[psobject]或[object]改為,然后從現有的分組值創建結果物件:
$selectFunc = [Func[Object,psobject]]{
$sum=0
foreach($p in $args[0].price){
$sum = $p
}
# Output new object with first last based on input object sum
$args[0] |Select first,last,@{Name='sum';Expression={$sum}} -First 1
}
我想通過 Linq 盡可能快地做到這一點。
我強烈建議您實際測驗這是否比使用Group-Object或簡單的哈希表更快- 很多使 PowerShell 變慢的開銷(特別是引數系結)仍然適用于您的代碼,因此差異可能并不重要 - 但您的腳本的可讀性可能會受到嚴重影響。
我個人的偏好是只使用Group-Objectcmdlet:
$costs |Group-Object first,last |ForEach-Object {
$sum = ($_.Group |Measure price -Sum).Sum
$_.Group |Select -Property first,last,@{N='Sum';E={$sum}} -First 1
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405509.html
標籤:
