迭代 ICollection 时是否可以确定当前位置?

发布于 2024-10-12 12:00:57 字数 263 浏览 2 评论 0原文

我有以下代码:

foreach(string reelid in unValidatedFeedersOnMachine.Keys)
{
    _sqlString.Append("CompID = '").Append(reelid).Append("' ");
}

除了最后一次之外,我需要在每次迭代 .Appened("or ") 时添加该循环。

知道我如何知道我何时在这里进行最后一次迭代吗?

I have the following code:

foreach(string reelid in unValidatedFeedersOnMachine.Keys)
{
    _sqlString.Append("CompID = '").Append(reelid).Append("' ");
}

I need to add in that loop on each iteration .Appened("or ") except the last one.

Any idea how i can know when i located on the last iteration here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

俏︾媚 2024-10-19 12:00:57

我会用另一种方式来做 - 将第一个视为异常更简单:

bool first = true;
foreach(string reelid in unValidatedFeedersOnMachine.Keys)
{
   if(first) {first = false;}
   else {_sqlString.Append(" or ";}
    _sqlString.Append("CompID = '").Append(reelid).Append("' ");
}

或者在 .NET 4.0 中只需使用:

string s = string.Join(" or ",
           from key in unValidatedFeedersOnMachine.Keys
           select "CompID = '" + reelid + "'");

甚至更好,如果这是 SQL - 切换到 IN...

string s = "CompID IN (" + string.Join(","
           from key in unValidatedFeedersOnMachine.Keys
           select "'" + reelid + "'") + ")";

I would do it the other way around - treating the first as the exception is simpler:

bool first = true;
foreach(string reelid in unValidatedFeedersOnMachine.Keys)
{
   if(first) {first = false;}
   else {_sqlString.Append(" or ";}
    _sqlString.Append("CompID = '").Append(reelid).Append("' ");
}

or in .NET 4.0 just use:

string s = string.Join(" or ",
           from key in unValidatedFeedersOnMachine.Keys
           select "CompID = '" + reelid + "'");

or even better, if this is SQL - switch to IN...

string s = "CompID IN (" + string.Join(","
           from key in unValidatedFeedersOnMachine.Keys
           select "'" + reelid + "'") + ")";
℉絮湮 2024-10-19 12:00:57

如果在一条线上完成所有操作呢?

string query = string.Join(" or ", unValidatedFeedersOnMachine.Keys.Select(x => "CompID = '" + x + "'").ToArray())

PS
如果您的目标是 .net 4.0,则可以跳过 .ToArray()

What about doing all in one line ?

string query = string.Join(" or ", unValidatedFeedersOnMachine.Keys.Select(x => "CompID = '" + x + "'").ToArray())

P.S.
If you're targeting .net 4.0, you can skip the .ToArray()

寄离 2024-10-19 12:00:57

我倾向于这样做

var _sqlString = new StringBuilder();
foreach(string reelid in unValidatedFeedersOnMachine.Keys) {                     
    if(_sqlString.ToString().Length != 0) {
        _sqlString.Appened(" or ")
    }
    _sqlString.Append("CompID = '").Append(reelid).Append("' ");
}

I tend to do this

var _sqlString = new StringBuilder();
foreach(string reelid in unValidatedFeedersOnMachine.Keys) {                     
    if(_sqlString.ToString().Length != 0) {
        _sqlString.Appened(" or ")
    }
    _sqlString.Append("CompID = '").Append(reelid).Append("' ");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文