Using LINQ Repeat To Generate Copies Of Objects
[C#, .NET, LINQ]
You will also occasionally have a situation where you need to clone an object several times.
Let us take, for example, the following type:
public sealed record Agent
{
public required string Name { get; init; }
public required DateOnly DateOfBirth { get; init; }
}
Suppose, for whatever reason, we need 100 copies of the agent James Bond, perhaps for testing purposes.
There are several ways to do it.
One way to do this is to combine Enumerable.Range to generate a sequence and then use that sequence to project. Like so:
// First create an enumerable of 100 numbers, 0 - 99
var jamesBonds = Enumerable.Range(0, 100)
// Project into an Agent object, but since we're not using the numbers,
// Discard them using the discard _
.Select(_ => new Agent { Name = "James Bond", DateOfBirth = new DateOnly(1970, 1, 1) }).ToList();
This will give us a list of 100 James Bonds.
There is a simpler way to do this - using the LINQ Repeat method.
var jamesBonds = Enumerable.Repeat(new Agent { Name = "James Bond", DateOfBirth = new DateOnly(1970, 1, 1) }, 100)
.ToList();
Much shorter, terser, and clearer.
TLDR
LINQ
offers the Repeat
method to assist in duplicating objects a required number of times.
The code is in my GitHub.
Happy hacking!