在没有 for 循环的情况下为数据表中的所有行设置值 [英] Set value for all rows in a datatable without for loop
问题描述
我试图在不使用 for 循环的情况下为数据表中的单个列的所有行设置相同的值.任何人都可以建议任何更快的方法来实现相同的目标.
I'm trying to set the same value for all rows for a single column in a datatable without using for loop. Can anyone suggest any faster methods to achieve the same.
推荐答案
除非你算上 foreach
.无论哪种方式,您都需要循环.
Not unless you count foreach
. One way or another, you'll need to loop.
如果您使用 DataTable
版本,最快的方法是使用 DataColumn
访问器,即
If you use the DataTable
version, the fastest approach is to use the DataColumn
accessor, i.e.
var col = table.Columns["Foo"];
foreach(var row in table.Rows)
row[col] = value;
作为替代:因为这可能与数据库有关,手动编写 TSQL 以适当地设置所有值(即在 TSQL 中使用 suitable where
子句).
As an alternative : since this presumably relates to a database, write the TSQL manually to set all the values appropriately (i.e. with a suitable where
clause in the TSQL).
update [theTable]
set [theColumn] = theValue
where --TODO - something sensible
这篇关于在没有 for 循环的情况下为数据表中的所有行设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!