.NET c# 如何使用 linq2db 批量插入?

.NET c# How to mass insert with linq2db?

我目前正在使用 Linq2db 通过我的 C# 应用程序管理我的 sqlite 数据库。现在我正在读取一个包含 24k+ 行的 excel 文件,我想知道如何加快我的 ETL 过程?

for (int row = start.Row; row <= end.Row; row++)
        {
            if (row == 1) // Title row
                continue;

            Stock stock = new Stock(Processor.GetStore(workSheet.Cells[row, 1].Text),
                            Processor.GetProduct(workSheet.Cells[row, 2].Text),
                            int.Parse(workSheet.Cells[row, 6].Text),
                            int.Parse(workSheet.Cells[row, 7].Text),
                            int.Parse(workSheet.Cells[row, 8].Text), 0, true);

            Processor.AddStock(stock, false);

        }

使用 linq 尝试了不同的方法,但我得到的计时结果更差...

            var stocks = (from cell in workSheet.Cells["a:h"]
                      select new Stock(Processor.GetStore(workSheet.Cells[cell.Start.Row, 1].Text),
                            Processor.GetProduct(workSheet.Cells[cell.Start.Row, 2].Text),
                            int.Parse(workSheet.Cells[cell.Start.Row, 6].Text),
                            int.Parse(workSheet.Cells[cell.Start.Row, 7].Text),
                            int.Parse(workSheet.Cells[cell.Start.Row, 8].Text), 0, true)).ToList();

我要找的是这样的:

public static void MassStockInsert(List<Stock> stocks)
    {
        using (var db = new Processor())
        {
            db.Stock
                .insert(stocks);
        }
        Processor.Stocks = ReloadStocks();
    }

使用批量复制方法。

public static void MassStockInsert(List<Stock> stocks)
    {
        using (var db = new Processor())
        {
            db.BulkCopy(stocks);
        }
        Processor.Stocks = ReloadStocks();
    }