我有一个获取实体集合的函数,然后将引号和逗号附加到字符串以更新数据库中的集合。这会花费很多时间,效率很低,但是我想不出另一种方法:
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();
}
}
最佳答案
大概是这样吗?
var str = String.Join(",", transes.Select(t => string.Format("'{0}'", t.GUID)))
但是,由于您的代码中有注释,该注释与
> 750
记录一起超时,因此“疯狂的时间”可能来自数据库,而不是代码。String.Join是一个非常方便的方法,当您想将一系列东西连接在一起时,因为它会自动为您处理两端(因此您不会以前导或尾随定界符结尾)。
关于c# - 替代foreach循环和字符串生成器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35016062/