给定代码的优化版本是什么?
我有一段代码,我在其中检查复选框,比较给定代码中数据表的内容:
foreach (DataRow dr in dtResult.Rows)
{
for (var i = 0; i < chkboxListWorkTypes.Items.Count; i++)
{
if (chkboxListWorkTypes.Items[i].Value.Equals(dr["WorkTypeID"].ToString()))
{
chkboxListWorkTypes.Items[i].Selected = true;
}
}
}
任何 labmda 或 linq 表达式都很棒。
I have a block of code where i am checking checkboxes, comparing the contents of the datatable in the given code:
foreach (DataRow dr in dtResult.Rows)
{
for (var i = 0; i < chkboxListWorkTypes.Items.Count; i++)
{
if (chkboxListWorkTypes.Items[i].Value.Equals(dr["WorkTypeID"].ToString()))
{
chkboxListWorkTypes.Items[i].Selected = true;
}
}
}
Any labmda or linq expression would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
真正的优化将是数据绑定。
Real optimization will be a data binding.
我就会这样做,
如果复选框上的 ID 不同(可能是不同),
This is what I do
if the ID on the checkboxes is diferent (that probably is),
尽管它在幕后的作用与 @Aristos 的回答几乎相同,但按以下方式执行会减少代码行数:
Though it does practically the same thing behind the scenes as @Aristos answer, doing it like the following will cut your lines of code down a bit:
您本质上是在执行 JOIN 操作。有 3 种类型常用的连接算法 - 嵌套循环、散列和合并。您的代码正在使用嵌套循环算法 - 这似乎适合 10 和 5 的表大小,除非其中一个或两个已经排序(在这种情况下,合并联接可能更合适)。
我无法想象这会成为任何现实应用程序中的瓶颈 - 但是,我们可以通过一些假设对其进行一些改进。
<前><代码>
int chkBoxCount = chkboxListWorkTypes.Items.Count;
foreach(dtResult.Rows 中的 DataRow dr)
{
字符串 rowValue = dr["WorkTypeID"].ToString();
for (var i = 0; i < chkBoxCount; i++)
{
var chkBox = chkboxListWorkTypes.Items[i];
if (chkBox.Value.Equals(rowValue))
{
chkBox.Selected = true;
}
}
}
<前><代码>
int chkBoxCount = chkboxListWorkTypes.Items.Count;
foreach(dtResult.Rows 中的 DataRow dr)
{
字符串 rowValue = (字符串)dr["WorkTypeID"];
for (var i = 0; i < chkBoxCount; i++)
{
var chkBox = chkboxListWorkTypes.Items[i];
if (chkBox.Value.Equals(rowValue))
{
chkBox.Selected = true;
}
}
}
<前><代码>
int chkBoxCount = chkboxListWorkTypes.Items.Count;
foreach(dtResult.Rows 中的 DataRow dr)
{
字符串 rowValue = (字符串)dr["WorkTypeID"];
for (var i = 0; i < chkBoxCount; i++)
{
var chkBox = chkboxListWorkTypes.Items[i];
if (!chkBox.Selected && chkBox.Value.Equals(rowValue))
{
chkBox.Selected = true;
}
}
}
<前><代码>
int chkBoxCount = chkboxListWorkTypes.Items.Count;
foreach(dtResult.Rows 中的 DataRow dr)
{
字符串 rowValue = (字符串)dr["WorkTypeID"];
for (var i = 0; i < chkBoxCount; i++)
{
var chkBox = chkboxListWorkTypes.Items[i];
if (!chkBox.Selected && chkBox.Value.Equals(rowValue))
{
chkBox.Selected = true;
休息; // 停止寻找
}
}
}
<前><代码>
int chkBoxCount = chkboxListWorkTypes.Items.Count;
for (var i = 0; i < chkBoxCount; i++)
{
var chkBox = chkboxListWorkTypes.Items[i];
if (chkBox.Selected) 继续;
foreach(dtResult.Rows 中的 DataRow dr)
{
字符串 rowValue = (字符串)dr["WorkTypeID"];
if (chkBox.Value.Equals(rowValue))
{
chkBox.Selected = true;
休息;
}
}
}
<前><代码>
int chkBoxCount = chkboxListWorkTypes.Items.Count;
for (var i = 0; i < chkBoxCount; i++)
{
var chkBox = chkboxListWorkTypes.Items[i];
if (chkBox.Selected) 继续;
foreach(dtResult.Rows 中的 DataRow dr)
{
字符串 rowValue = (字符串)dr["WorkTypeID"];
if (string.Equals(chkBox.Value, rowValue, StringComparison.OrdinalIgnoreCase))
{
chkBox.Selected = true;
休息;
}
}
}
这就是我能做的最好的事情了。如果在如此小的数据集上存在任何可测量的差异,我会感到惊讶。所以,这就是你真正应该做的:
或者,使用 Each 扩展:
这可能会稍微慢一些,但在我看来读起来要好得多。
You're essentially performing a JOIN operation. There are 3 types of join algorithms in common use - nested loop, hash, and merge. Your code is using the nested loop algorithm - which seems appropriate for table sizes of 10 and 5, unless one or both are already sorted (in which case, a merge join may be more appropriate).
I can't imagine this being a bottleneck in any real-world application - but, we can possibly improve it a little bit with some assumptions.
That's about the best I can do. I'd be surprised if there was any measurable difference on such a small dataset. So, here's what you should really do:
Or, with an Each extension:
which may be slightly slower, but reads a lot better IMO.