r/PowerShell • • Apr 12 '26

Script Sharing 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

https://github.com/Metekillot/LinqLinker

https://www.powershellgallery.com/packages/LinqLinker/1.0.6


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
selected -> hello
selected -> ,
selected -> world!

OfType check

foo

Numerical performance

Name           Ticks
----           -----
Standard      318901
LinqLink[int] 109493
17 Upvotes

16 comments sorted by

View all comments

Show parent comment

1

u/spikeyfreak Apr 12 '26 edited Apr 12 '26

Right, so this explains what I was asking and I suspected I didn't understand something.

Which is why this evaluates to false.

$Val = 0
[bool]($var = $val)

I can't figure out any other reason than $val is 0, which in boolean is false, but this is strange behavior that I don't understand. I guess I always just assumed when you did something in a conditional expression, the $? from that command is what was evaluated (edit: if there is no output from the command).

Edit: I'm just confusing myself more.

So it evaluates to false because of this:

PS C:\> ($var = $val)
0
PS C:\> $var = $val
PS C:\>

Why does it output the value if you put them in parens?

1

u/Thotaz Apr 12 '26

Why does it output the value if you put them in parens?

Good question, I don't know for sure, but I think it's actually the other way around. The default behavior for assignment statements is to output something unless they are inside an expression/script block. It happens in if statements. It happens in loops. It happens in chained assignment expressions like: [bool]$BoolVal = [int]$IntVal = [double]$DoubleVal = [string]$StringVal = "0.5"