3

i'm trying to do the following, but not sure how ...

var foo = new Foo
    {
        Id = MyRandom<int>(1, 100),
        Name = MyRandom<string>(5,20),
        MyPets = MyRandom<bool>() ?
            new IList<Pet>
            (petList =>
                { 
                   var x = MyRandom<int>(1, 4);
                   for (int i = 0; i < x; i++)
                   {
                       petList.Add(new Pet(MyRandom<string>(1,15));
                   }
                }
            : null
    };

so .. this creates a random list of pets.

Any ideas?

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

2 Answers2

3

No.

Instead, you can create a lambda expression, then call it immediately:

MyRandom<bool>() ? null : (new Func<IList<Pet>>(() => { return ... })()
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Have you tried something like this?

var foo = new Foo
    {
        Id = MyRandom<int>(1, 100),
        Name = MyRandom<string>(5,20),
        MyPets = MyRandom<bool>() ?
            Enumerable.Range(0, MyRandom<int>(1, 4))
                .Select(_ => new Pet(MyRandom<string>(1,15)))
                .ToList()
            : null
    };
dahlbyk
  • 75,175
  • 8
  • 100
  • 122