You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.Net Linq查询转换为PowerShell 5.1静态Linq方法实现咨询

PowerShell 调用静态LINQ方法实现分组校验的方案

核心语法转换规则

PowerShell调用System.Linq.Enumerable静态方法的核心适配逻辑:

  • 泛型方法需要先通过MakeGenericMethod构造绑定具体参数类型,再用Invoke执行
  • VB.Net里的Lambda表达式对应PowerShell中强类型的Func<>封装脚本块
  • VB.Net的New With匿名类可替换为Tuple(自带等值比较,适合去重场景)或PSCustomObject(需要自定义属性名场景)

完整实现代码

假设原VB.Net实现逻辑为按AttributeKey分组,校验同组内AttributeValue+AttributeType组合是否唯一,对应PowerShell静态LINQ实现如下:

# 加载依赖程序集(PowerShell默认已加载,异常时可主动执行)
Add-Type -AssemblyName System.Data
Add-Type -AssemblyName System.Core

# 替换为你的目标DataTable实例
$dt = [你的DataTable对象]

# 1. DataTable转IEnumerable<DataRow>,对应VB.Net的AsEnumerable()
$asEnumerableMethod = [System.Data.DataTableExtensions].GetMethod("AsEnumerable", [Type[]]@([System.Data.DataTable]))
$rowEnumerable = $asEnumerableMethod.Invoke($null, @($dt))

# 2. 按AttributeKey分组,对应VB.Net的GroupBy方法
$groupByMethod = [System.Linq.Enumerable].GetMethods() | 
    Where-Object { $_.Name -eq "GroupBy" -and $_.GetParameters().Count -eq 2 } | 
    Select-Object -First 1
$groupByGeneric = $groupByMethod.MakeGenericMethod([System.Data.DataRow], [string])
$keySelector = [Func[System.Data.DataRow, string]]{ param($row) $row["AttributeKey"].ToString() }
$keyGroups = $groupByGeneric.Invoke($null, @($rowEnumerable, $keySelector))

# 3. 筛选同组内Value+Type不唯一的非法分组
$whereMethod = [System.Linq.Enumerable].GetMethods() | 
    Where-Object { $_.Name -eq "Where" -and $_.GetParameters().Count -eq 2 } | 
    Select-Object -First 1
$whereGeneric = $whereMethod.MakeGenericMethod([System.Linq.IGrouping[string, System.Data.DataRow]])
$groupCheckPredicate = [Func[System.Linq.IGrouping[string, System.Data.DataRow], bool]]{
    param($group)
    # 投影为Value+Type的Tuple组合
    $selectMethod = [System.Linq.Enumerable].GetMethods() | 
        Where-Object { $_.Name -eq "Select" -and $_.GetParameters().Count -eq 2 } | 
        Select-Object -First 1
    $selectGeneric = $selectMethod.MakeGenericMethod([System.Data.DataRow], [Tuple[string, string]])
    $valueTypeSelector = [Func[System.Data.DataRow], [Tuple[string, string]]]{
        param($row)
        [Tuple]::Create($row["AttributeValue"].ToString(), $row["AttributeType"].ToString())
    }
    $valueTypeList = $selectGeneric.Invoke($null, @($group, $valueTypeSelector))
    # 去重后计数大于1即为非法分组
    $distinctMethod = [System.Linq.Enumerable].GetMethods() | 
        Where-Object { $_.Name -eq "Distinct" -and $_.GetParameters().Count -eq 1 } | 
        Select-Object -First 1
    $distinctGeneric = $distinctMethod.MakeGenericMethod([Tuple[string, string]])
    $distinctCount = [System.Linq.Enumerable]::Count($distinctGeneric.Invoke($null, @($valueTypeList)))
    return $distinctCount -gt 1
}
# 得到所有非法分组结果
$invalidGroups = $whereGeneric.Invoke($null, @($keyGroups, $groupCheckPredicate))

# 遍历输出校验结果
foreach ($g in $invalidGroups) {
    Write-Host "非法AttributeKey:$($g.Key),同Key下存在多组不同的AttributeValue/AttributeType组合"
}

内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 06:15:05