GetRecords结果的foreach失败,但ToList()解决了

时间:2019-02-19 11:58:09

标签: c# .net csvhelper

有人可以告诉我为什么吗

        int size = 3;

        DocX docX = DocX.Create(result, DocumentTypes.Document);

        Table table = docX.AddTable(size, size);

        table.AutoFit = AutoFit.Contents;            

        for (int i = 0; i <= (int)TableBorderType.InsideV; i++)
            table.SetBorder((TableBorderType)i, new Border());


        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++)
                table.Rows[i].Cells[j].Paragraphs[0].InsertText(i + " | " + j);



        docX.InsertParagraph().InsertTableBeforeSelf(table);
        docX.Save();

失败了,这:

var rows = csv.GetRecords<TEntity>();
foreach (var row in rows)
{
}

成功?

编辑

var rows = csv.GetRecords<TEntity>().ToList();
foreach (var row in rows)
{
}

在上面的示例中删除ToList(),并且test方法中的foreach失败。 Test方法使用Entity Framework类来接收数据。

这是.csv文件中数据的一部分:

public static IEnumerable<TEntity> GetData<TEntity>(string basePath)
    where TEntity : class
{
    var entityName = typeof(TEntity).Name;
    var fileName = Path.Combine(basePath, $"{entityName}.csv");

    Console.WriteLine($"Reading: {fileName}");

    using (var reader = new StreamReader(fileName))
    using (var csv = new CsvReader(reader))
    {
        csv.Configuration.HeaderValidated = null;
        csv.Configuration.MissingFieldFound = null;
        csv.Configuration.IgnoreReferences = true;
        var rows = csv.GetRecords<TEntity>().ToList();

        return rows;
    }
}

[TestMethod()]
public void GetDataTest()
{
    var rows = DbData.GetData<EP3_RASGEMIDDELDEN>(BasePath);

    foreach (var row in rows)
    {

    }
}

0 个答案:

没有答案