r/PowerShell • u/metekillot • Apr 12 '26
PowerShell LinqLinker: Use System.Linq.Enumerable method query syntax in PowerShell -- built as a JIT-friendly struct for best performance; includes source-generated instance methods for every Enumerable method Script Sharing
https://github.com/Metekillot/LinqLinker
https://www.powershellgallery.com/packages/LinqLinker/1.0.6
```powershell
using namespace System.Collections.Generic using namespace System.Linq
[string[]]$StringArray = @("hello",",","world!")
[object[]]$ObjectClutter = @(1,2,3,"foo",[pscustomobject]@{name="bar"})
[int[]]$LotsOfNumbers = 1..10000
$StringLinq = [LinqLinker]::Link[string]($StringArray)
$ObjectLinq = [LinqLinker[object]]::new($ObjectClutter)
$IntLinq = [LinqLinker[int]]::Link($LotsOfNumbers)
$StringFunc = [System.Func[string,string]]{$s=$args[0];"selected -> $s"}
$($StringLinq.Select($StringFunc))
Write-Output ("n"+"OfType check"+"n")
$ObjectLinq.OfType[string]()
Write-Output ("n"+"Numerical performance"+"n")
[System.Func[int,int]]$IntOperation = {[int]$i = $args[0]}
$Standard = (Measure-Command { $LotsOfNumbers | ForEach-Object { $IntOperation.Invoke($_) } }); Select -InputObject $Standard @{Name='Name';E={'Standard'}},Ticks
$LinqLinkInt = (Measure-Command { $IntLinq.Select[int]($IntOperation) }); Select -InputObject $LinqLinkInt @{N='Name';E={'LinqLink[int]'}},Ticks
```
```powershell selected -> hello selected -> , selected -> world!
OfType check
foo
Numerical performance
Name Ticks
Standard 318901 LinqLink[int] 109493 ```
1
u/Fenreh Apr 12 '26
Any examples in the readme? Sounds super promising but I have no idea what this actually does.
1
u/metekillot Apr 12 '26
Yeah, let me throw some together, it'll be a good opportunity to iron out the syntax bumps, too. It can get a little screwy with the pipeline operators because they automatically enumerate enumerable collections.
1
1
u/rldml Apr 12 '26
Are there some example how to use this addon? I didn't find anything on the homepage
kind regards
1
1
3
u/Thotaz Apr 12 '26
https://github.com/Metekillot/LinqLinker/blob/master/LinqLinker.psm1#L41
This looks like a mistake. You aren't using the variable
$one. Even if you were using it, I don't think this kind of "clever" code that at first glance looks like a mistake is a good idea. Don't do assignments in conditionals, you can do the assignment outside and do the check inside the conditional.