Search code examples
c#.netfakerbogus

How to generate fixed number of items out of total items with same value in Bogus C#


I am trying to generate 3000 fake records in C# with condition that each 1000 items will have same time stamp(update_time) in UTC milliseconds, then next 1000 will have same time stamp in UTC milliseconds. how to achieve that?

private static IReadOnlyCollection<Document> GetDocumentsToInsert()
        {
            return new Bogus.Faker<Document>()
            .StrictMode(true)
            //Generate item
            .RuleFor(o => o.id, f => Guid.NewGuid().ToString()) //id
            .RuleFor(o => o.update_time, f => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();)
            .Generate(3000);
        }

        // <Model>
        

        public class Document
        {
            public string update_time {get;set;}
            public string id{get;set;}
        }

Solution

  • I am not familiar with Faker but it looks like you want something like:

    private static IEnumerable<Document> GetDocumentsToInsert()
    {
        IEnumerable<Document> result = new List<Document>();
    
        for (int x = 0; x < 3; ++x)
        {
            DateTimeOffset timeNow = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
    
            IEnumerable<Document> temp = new Bogus.Faker<Document>()
                .StrictMode(true)
                //Generate item
                .RuleFor(o => o.id, f => Guid.NewGuid().ToString()) //id
                .RuleFor(o => o.update_time, f => timeNow;)
                .Generate(1000);
    
            result = result.Concat(temp);
        }
    
        return result;
    }