如何检测/找到while循环的结束?

3

我正在使用C#执行一个“while”循环,这个循环遍历从数据库中检索出来的一些记录。如何最好地检测/查找循环中的最后一条记录?这可能吗?

这是我的代码:

    while (sdr.Read())
    {
        //Pull each line from DB
        the_title = sdr["the_title"].ToString();
        the_cats = sdr["the_category"].ToString();
        the_tags = sdr["the_tags"].ToString();
        the_date = sdr["the_date"].ToString();

        //Start file creation
        writer.WriteLine("[");
        writer.WriteLine("\"" + the_title + "\", ");
        writer.WriteLine("\"" + the_cats + "\", ");
        writer.WriteLine("\"" + the_tags + "\", ");
        writer.WriteLine("\"" + the_date + "\", ");
        writer.WriteLine("\"<a href=\\\"#\\\" class=\\\"sepV_a\\\" title=\\\"Edit\\\"><i class=\\\"icon-pencil\\\"></i></a>\"");

        writer.WriteLine("],");

    }
    writer.WriteLine("]");
    writer.WriteLine("}");
    writer.Close();

我遇到的问题在于最后一行代码 "writer.WriteLine("],");",我需要删除从数据库中获取的最后一条记录上的逗号。

谢谢


@EvanMulawski - 可能是一个 SqlDataReader - Oded
提示,使用verbatim字符串可以避免所有这些反斜杠:@"\foo..." - gdoron
是的,它是SqlDataReader。 - jorame
5个回答

3
反过来做:
bool is_first = true;

while (sdr.Read()) {

    if (is_first) {
        is_first = false;
    } else {
        writer.Write(",");
    }

    // Do your other writes here
}

它的效率和 writer.WriteLine("["); 一样高。我不想浪费时间运行基准测试,但我相当有信心差异将是可以忽略不计的。 - Sean Bright
1
我理解分支预测。而且因为 is_first 不是不可预测的(它在第一次迭代时为 true,在其余迭代中为 false),所以它不会像你所暗示的那样对性能产生负面影响。 - Sean Bright

2
我建议你只删除最后一个字符。这是循环内最有效的解决方案。
StringBuilder sb = new StringBuilder();

    while (sdr.Read())
    {
       sb.Append("Value");
       ....
    }

if(sb.Length > 0)
{
sb.Remove(sb.Length - 1, 1)
}
var result = sb.ToString();

0

另一个应该可行的方法:

List<String> bufferList = new List<String>();
while (sdr.Read())
{
    //Pull each line from DB
    the_title = sdr["the_title"].ToString();
    the_cats = sdr["the_category"].ToString();
    the_tags = sdr["the_tags"].ToString();
    the_date = sdr["the_date"].ToString();

    StringBuilder tempSb = new StringBuilder();
    tempSb.AppendLine();

    //Start file creation
    tempSb.AppendLine("[");
    tempSb.AppendLine("\"" + the_title + "\", ");
    tempSb.AppendLine("\"" + the_cats + "\", ");
    tempSb.AppendLine("\"" + the_tags + "\", ");
    tempSb.AppendLine("\"" + the_date + "\", ");
    tempSb.AppendLine("\"<a href=\\\"#\\\" class=\\\"sepV_a\\\" title=\\\"Edit\\\"><i class=\\\"icon-pencil\\\"></i></a>\"");
    tempSb.AppendLine(("]");

    bufferList.Add(tempSb.ToString());

}

String.Join(",", bufferList);

0

另一种方法

if(sdr.Read()) {
    while (true) {
        ...
        writer.WriteLine("[");  
        ...
        if (!sdr.Read()) {
            writer.WriteLine("]"); 
            break;
        }
        writer.WriteLine("],");  
    }
}

-1

在达到或超过最后一个之前,你无法知道它是什么,但你可以知道第一个。你可以修改你的代码如下:

bool isFirst = true;
while (sdr.Read())
{
    if (isFirst) isFirst = false;
    else writer.WriteLine(",");

    //Pull each line from DB
    the_title = sdr["the_title"].ToString();
    the_cats = sdr["the_category"].ToString();
    the_tags = sdr["the_tags"].ToString();
    the_date = sdr["the_date"].ToString();

    //Start file creation
    writer.WriteLine("[");
    writer.WriteLine("\"" + the_title + "\", ");
    writer.WriteLine("\"" + the_cats + "\", ");
    writer.WriteLine("\"" + the_tags + "\", ");
    writer.WriteLine("\"" + the_date + "\", ");
    writer.WriteLine("\"<a href=\\\"#\\\" class=\\\"sepV_a\\\" title=\\\"Edit\\\"><i class=\\\"icon-pencil\\\"></i></a>\"");

    writer.Write("]");
}
writer.WriteLine();

否则,为了避免在循环的每个实例中进行检查,您可以使用:

var sb = new StringBuilder();
while (sdr.Read())
{
    //Pull each line from DB
    the_title = sdr["the_title"].ToString();
    the_cats = sdr["the_category"].ToString();
    the_tags = sdr["the_tags"].ToString();
    the_date = sdr["the_date"].ToString();

    //Start file creation
    sb.AppendLine("[");
    sb.AppendLine("\"" + the_title + "\", ");
    sb.AppendLine("\"" + the_cats + "\", ");
    sb.AppendLine("\"" + the_tags + "\", ");
    sb.AppendLine("\"" + the_date + "\", ");
    sb.AppendLine("\"<a href=\\\"#\\\" class=\\\"sepV_a\\\" title=\\\"Edit\\\"><i class=\\\"icon-pencil\\\"></i></a>\"");

    sb.AppendLine("],");
}
if (sb.Length > 0)
{
    // Write result, sans characters for last newline (Environment.NewLine) and comma.
    writer.WriteLine(sb.ToString(0, sb.Length - (Environment.NewLine.Length + 1));
}

编辑:通过使用Environment.NewLine.Length使剪辑长度动态化。


我需要在每个代码块的行末保留逗号,但是在最后一行不需要。这个代码可以实现吗? - jorame
@jorame 抱歉,我忘记打逗号了。现在已经修复了。我需要再次确认Length - 2是否是正确的剪裁长度,但除此之外,这个代码可以正常运行。 - Mike Guthrie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接