【问题标题】:Does foreach have else?foreach 还有其他的吗?
【发布时间】:2012-08-11 12:22:29
【问题描述】:

我有 foreach 来显示来自 myquery 的结果,当结果不为空时它可以工作。我想知道foreach还有其他吗?当 myquery 的结果为空时采取措施?

var rs = from cs in db.CsCustomers
                      from ai in db.ArInvoices
                      where cs.CustomerID == ai.CustomerID &&
                      ai.Kategori != null
                      orderby cs.Unit
                      select new
                      {
                          cs.CustomerID,
                          cs.Unit,
                          cs.Name
                      };

foreach (var r in rs)
{
     c = new TableCell();
     c.Text = r.CustomerID;
     c.RowSpan = jk;
     tr.Cells.Add(c);

     c = new TableCell();
     c.Text = r.Unit;
     c.RowSpan = jk;
     tr.Cells.Add(c);

     c = new TableCell();
     c.Text = r.Name;
     c.RowSpan = jk;
     tr.Cells.Add(c);

}

【问题讨论】:

  • 您可能想要添加一个方法来删除所有重复的代码c = new TableCell(); c.Text = r.CustomerID; c.RowSpan = jk; tr.Cells.Add(c);
  • 如果你仔细想想……那没有多大意义。 Foreach,字面意思是“为某物集合中的每件事物(做某事)”。下面的人给出的答案是“(do something) for every things in a collection of things where something is not null”..希望这对你有意义。
  • @SimonWhitehead 不,他们没有,他们检查 somethingS 集合是否为空,然后做其他事情,而不是单独做某事。

标签: c# foreach if-statement dbnull


【解决方案1】:

不,它没有 else 运算符。您可以将其包装在 if else

var rs = from cs in db.CsCustomers
                      from ai in db.ArInvoices
                      where cs.CustomerID == ai.CustomerID &&
                      ai.Kategori != null
                      orderby cs.Unit
                      select new
                      {
                          cs.CustomerID,
                          cs.Unit,
                          cs.Name
                      };
if(rs != null && rs.Any()) //for the .Count you have to add using System.Linq;
     foreach (var r in rs)
     {
          c = new TableCell();
          c.Text = r.CustomerID;
          c.RowSpan = jk;
          tr.Cells.Add(c);

          c = new TableCell();
          c.Text = r.Unit;
          c.RowSpan = jk;
          tr.Cells.Add(c);

          c = new TableCell();
          c.Text = r.Name;
          c.RowSpan = jk;
          tr.Cells.Add(c);
     }
}else{
     //the else part 
}

【讨论】:

  • 您可以使用 .Any() 而不是 Count() > 0。另外,我认为你的||应该是 &&
【解决方案2】:

也许你会需要这个。无论如何,如果您经常需要空列表的默认行为,您可以创建下一个扩展方法。

void Main()
{
    var elements = Enumerable.Range(0,20);
    elements.ForEachWithElse(x=>Console.WriteLine (x),
                            ()=>Console.WriteLine ("no elements at all"));

    elements.Take(0).ForEachWithElse(x=>Console.WriteLine (x),
                            ()=>Console.WriteLine ("no elements at all"));
}

public static class MyExtensions
{    
    public static void ForEachWithDefault<T>(this IEnumerable<T> values, Action<T> function, Action emptyBehaviour)
    {
        if(values.Any())
            values.ToList().ForEach(function);
        else 
            emptyBehaviour();
    }
}

【讨论】:

    【解决方案3】:

    Here is an article 了解如何实现某些东西。

    这是您每天在 IEnumerable 上的常规 foreach,但允许您 指定“循环动作”和“其他动作”,后者是 在循环之后执行,除非您通过返回 false 来中断循环 从前者。它比 Python 有点笨拙,但仍然有用

    您可以做的只是使用if 测试检查它是否为空/空,然后在else 部分中执行某些操作。 但是,在做 DB 的事情时,最好在没有结果的时候返回一个空列表,而不是返回 null。

    【讨论】:

      【解决方案4】:

      不,没有 foreach-else 循环...如果您需要该功能,请使用不同类型的循环(for-loop 或 do-while-loop)或先测试元素的数量。

      【讨论】:

        【解决方案5】:

        为什么不先检查 -

        if(rs == null)
        {}
        else
        {
          foreach.....
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-02-20
          • 2010-11-03
          • 2011-04-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多