C# 使用Fixture是否可以创建N个对象的列表?

C# 使用Fixture是否可以创建N个对象的列表?,c#,fixture,C#,Fixture,我想用Fixture创建一个包含N个对象的列表 我知道我可以用: List<Person> persons = new List<Person>(); for (int i = 0; i < numberOfPersons; i++) { Person person = fixture.Build<Person>().Create(); persons.Add(person); } List persons=新列表(); for(in

我想用Fixture创建一个包含N个对象的列表

我知道我可以用:

List<Person> persons = new List<Person>();

for (int i = 0; i < numberOfPersons; i++)
{
    Person person = fixture.Build<Person>().Create();
    persons.Add(person);
}
List persons=新列表();
for(int i=0;i

有没有什么方法可以使用
CreateMany()
方法或其他方法来避免循环?

找到了答案。CreateMany有一些获得“count”的重载

谢谢大家。

您可以使用linq:

  List<Person> persons = Enumerable.Range(0, numberOfPersons)
            .Select(x => fixture.Build<Person>().Create())
            .ToList();
List persons=可枚举范围(0,numberOfPersons)
.Select(x=>fixture.Build().Create())
.ToList();
我已经为人们做过了

/// <summary>
/// This is a class containing extension methods for AutoFixture.
/// </summary>
public static class AutoFixtureExtensions
{
    #region Extension Methods For IPostprocessComposer<T>

    public static IEnumerable<T> CreateSome<T>(this IPostprocessComposer<T> composer, int numberOfObjects)
    {
        if (numberOfObjects < 0)
        {
            throw new ArgumentException("The number of objects is negative!");
        }

        IList<T> collection = new List<T>();

        for (int i = 0; i < numberOfObjects; i++)
        {
            collection.Add(composer.Create<T>());
        }

        return collection;
    }

    #endregion
}
//
///这是一个包含AutoFixture扩展方法的类。
/// 
公共静态类AutoFixtureExtensions
{
#iposprocesscomposer的区域扩展方法
公共静态IEnumerable CreateSome(此IPostprocessComposer编写器,int numberOfObjects)
{
如果(numberOfObjects<0)
{
抛出新ArgumentException(“对象数为负数!”);
}
IList collection=新列表();
for(int i=0;i
var dtos=(新Fixture()).CreateMany(numberRecords);

是的,当然,您可以使用
CreateMany
作为下一个示例:

var numberOfPersons = 10; //Or your loop length number
var fixture = new Fixture();
var person = fixture.CreateMany<Person>(numberOfPersons).ToList(); 
//ToList() to change  the IEnumerable to List
var numberOfPersons=10//或您的循环长度编号
var fixture=新fixture();
var person=fixture.CreateMany(numberOfPersons.ToList();
//ToList()将IEnumerable更改为List

在发布问题后的100秒内,您是如何找到答案的!!:p当“count”为零时,它不会返回空集合,但会引发ExpException。执行不好。我现在就接受它。但是,我想要一个更固定的解决方案,而不是LINQ。不工作。它显示一条警告,指出Select有两个重载方法。谢谢。只使用夹具有解决方案吗?没问题,伙计。非常感谢。我将在48小时内接受答案。
var numberOfPersons = 10; //Or your loop length number
var fixture = new Fixture();
var person = fixture.CreateMany<Person>(numberOfPersons).ToList(); 
//ToList() to change  the IEnumerable to List