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

标签 c# linq sqlite 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();
    }

关于c# - .NET c# 如何使用 linq2db 批量插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34459169/

相关文章:

c# - .NET 中命名空间可以包含的类的数量是否有任何限制?

c# linq 如何检查我需要不为空的同一列中的空值

c# - 如何创建集合 (1 :n) relation

iphone - 带有 phonegap 的 IOS 上的 SQLite

c# - 在 Blazor wasm 中使用 CSharpScript 时出现 System.IO.FileNotFoundException

c# - CollectionViewSource 违反 MVVM

c# - 动态 Linq select 语句

python - 在 Python 的 sqlite3 中使用外键

c# - 从代码和网站调用Web API

c# - Linq-to-entities 中的 OrderBy、Select 和 Where 子句的顺序是否重要