var ids = new List<Guid>(count);
I have a multiple items empty list and I am looking for an elegant way to fill it with random Guids, without using for loop, preferably one-liner.
Inefficient but one line:
var list = Enumerable.Range(0, count).Select(_ => Guid.NewGuid()).ToList();
Much more efficient:
var list = new List<Guid>(count);
for (int i = 0 ; i < count ; i++) list.Add(Guid.NewGuid());
If the list already exists, then... just use the second version. You can probably force LINQ to do it without using a loop in your code, but: don't do that. You are looping here, so ... use a loop! Moving the loop into LINQ doesn't improve things - it just makes it harder to read and less efficient to execute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With