Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The PickRandom method should be extended with a Minimum and Maximum amount as well as returning NULL when zero is the Minimum amount #577

Open
TRoZZNL opened this issue Dec 5, 2024 · 0 comments

Comments

@TRoZZNL
Copy link

TRoZZNL commented Dec 5, 2024

Description

I want to be able to pass a collection to this method and have it return between X (minimum) and Y (maximum) items.
The method also accept an opt-in for returning NULL when the amount of picked items is zero.

LINQPad Code Example

var participantsEmpty = new Faker().PickRandom(people, 0, 5).ToList();

This would return between zero and five items from the people collection. The method will return an empty collection when the amount of items that will be returned is zero.

var participantsNull = new Faker().PickRandom(people, 0, 5, true).ToList();

This would return between zero and five items from the people collection. As we supply true as the third parameter, the method will return NULL when the amount of items that will be returned is zero.

What alternatives have you considered?

The following could be used as a workaround:

var faker = new Faker();
var attendeeCount = faker.Random.Number(2, 8);
var participantsEmpty = faker.PickRandom(people, attendeeCount).ToList();
someObject.Participants = participantsEmpty.Count == 0 ? NULL : participantsEmpty

Could you help with a pull-request?

Yes

TRoZZNL added a commit to TRoZZNL/Bogus that referenced this issue Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant