简体   繁体   English

foreach还有其他吗?

[英]Does foreach have else?

I have foreach to show the result from myquery, it works when the result isnt null. 我有foreach来显示myquery的结果,当结果不为null时它可以工作。 and I want to know does the foreach have else? 我想知道foreach还有其他吗? to take action when the result from myquery is null? 当myquery的结果为null时采取行动?

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);

}

Why not to put check first - 为什么不把支票放在首位-

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

No it does not have an else operator. 不,它没有else运算符。 You could wrap it in an if 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 
}

Maybe you will need this. 也许您会需要这个。 Anyway, in case you often need default behavior for empty lists, you may create next extension method. 无论如何,如果您经常需要为空列表使用默认行为,则可以创建下一个扩展方法。

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();
    }
}

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

Here is an article on how to implement something along the lines. 这是一篇有关如何大致实现某些内容的文章

It's your regular every day foreach on IEnumerable but allows you to specify both a "loop-action" and an "else-action" where the latter is executed after the loop unless you break the loop by returning false from the former. 这是您每天在IEnumerable上经常进行的学习,但允许您指定“循环动作”和“其他动作”,后者在循环后执行,除非您通过从前者返回false来中断循环。 It's a tad clumsier than Python but still useful 它比Python笨拙但仍然有用

What you can do instead is to just check it being null/empty with an if test, and then do something in the else part. 相反,您可以做的只是使用if测试检查它是否为空/空,然后在else部分中做一些else However, when doing DB stuff, it's better to return an empty list when there are no result, instead of returning null. 但是,在处理数据库时,最好在没有结果的情况下返回空列表,而不是返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM