The Bogus library has a helper method for selecting a random item in a collection:
public T PickRandom<T>(IEnumerable<T> items)
The method accepts IEnumerable
, which means you can create an Array or List<string>
to store the predefined data. You can use it together with the collection initializer to generate your phone list as follows:
var phones = new List<string> { "111-111-111", "222-222-222", "333-333-333" }; var Ids = 0; var test = new Faker<Person>() .RuleFor(p => p.Id, f => Ids++)
If you want to create many more entries in your list, and you do not want your initializer to increase (or you want to vary the number programmatically), you can use Linq:
You can find out more at readme on the GitHub project page.
source share