I found that using the postfix increment operator in Enumerable.Repeat doesn't increment the underlying variable. See for example:
Fiddle: https://dotnetfiddle.net/EnYStx
The output from above is:
Status IN (@0, @0)
When I would expect it to be:
Status IN (@0, @1)
What causes this behavior and is there a workaround aside from manually looping over the array?
Update
Apparently using a select works as expected:
But I'm still curious why it doesn't work with Enumerable.Repeat.
Update #2
I see why now. It is calling:
Once and then repeating that result n times.
Code:
var parameterIndex = 0;
var slugs = new[] {"one", "two"};
var slugParameters = Enumerable.Repeat($"@{parameterIndex++}", slugs.Length);
Console.WriteLine($" Status IN ({string.Join(", ", slugParameters)})");
The output from above is:
Quote:
Status IN (@0, @0)
Quote:
Status IN (@0, @1)
Update
Apparently using a select works as expected:
Code:
var slugParameters = slugs.Select(slug => $"@{parameterIndex++}");
Update #2
I see why now. It is calling:
Code:
$"@{parameterIndex++}"