替代 foreach 循环和字符串生成器

本文关键字:字符串 foreach 循环 替代 | 更新日期: 2023-09-27 18:32:00

我有一个函数,它获取实体集合,然后将引号和逗号附加到字符串以更新数据库中的集合。这需要花费大量的时间,效率非常低,但我想不出替代方案:

IEntityCollection c = Transactions.EvalToEntityCollection<ITransactions>(Store, key, item);
int max = transes.Count <= 750 ? transes.Count : 750;  // DB times out if there are more than 750, so 750 is the limit
int i = 0;
int t = transes.Count;
StringBuilder sb = new StringBuilder();
foreach (ITransactions trans in transes)
{
    sb.Append("'");
    sb.Append(trans.GUID);
    sb.Append("',");
    i++;
    t--;
    if (i == max || t == 0)
    {
        sb.Remove(sb.Length - 1, 1);
        //in here, code updates a bunch of transactions (if <=750 transaction)
        i = 0;
        sb = new StringBuilder();
    }
}

替代 foreach 循环和字符串生成器

也许是这样的东西?

var str = String.Join(",", transes.Select(t => string.Format("'{0}'", t.GUID)))

但是,由于代码中有注释,它超时> 750记录,因此您的"疯狂时间"可能来自数据库,而不是您的代码。

当您想要将内容列表连接在一起时,String.Join 是一种非常方便的方法,因为它会自动为您处理结尾(因此您最终不会使用前导或尾随分隔符)。

似乎你想这样做:

  1. 将交易编号分组为最多 750 个实体的批次
  2. 将所有这些交易编号放在一个组中,由逗号分隔并用单引号括起来

如果是这样,那么下面是构建批处理的代码:

const int batchSize = 750;
List<List<Transaction>> batches =
    transes
    .Select((transaction, index) => new { transaction, index })
    .GroupBy(indexedTransaction => indexedTransaction.index / batchSize)
    .Select(group => group.Select(indexedTransaction => indexedTransaction.transaction).ToList())
    .ToList();
foreach (var batch in batches)
{
    // batch here is List<Transaction>, not just the GUIDs
    var guids = string.Join(", ", batch.Select(transaction => "'" + transaction.GUID + "'"));
    // process transaction or guids here
}

字符串生成器是高效的。做 750 次(这是你的最大值)绝对不会比任何可用的技术花费更长的时间。

请注释掉 StringBuilder 部分并运行项目

sb.Append("'");
sb.Append("',");

我敢打赌,完成

所需的时间完全相同。