Does foreach have else?

11,091

Solution 1

Why not to put check first -

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

Solution 2

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

Solution 3

No it does not have an else operator. You could wrap it in an 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 
}
Share:
11,091
Firma Agnes
Author by

Firma Agnes

Updated on June 04, 2022

Comments

  • Firma Agnes
    Firma Agnes almost 2 years

    I have foreach to show the result from myquery, it works when the result isnt null. and I want to know does the foreach have else? to take action when the result from myquery is 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);
    
    }
    
  • Joey
    Joey over 11 years
    Rather than Count() > 0 you could use .Any(). Also, I think your || should be an &&