c# - foreach还有别的吗?

标签 c# foreach if-statement dbnull

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

}

最佳答案

为什么不先检查 -

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

关于c# - foreach还有别的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11965361/

相关文章:

C# 数据库访问 : DBNull vs null

c# - HtmlElement.SetAttribute 不适用于 webBrowser

c# - 连接到 .NET 3.1 Azure Function 项目 Startup.cs 中的 Entity Framework Core

swift - 需要帮助使用 foreach 在 swift ui 中增加数字

javascript - Vue.js ForEach 迭代语法问题

java - If - else 语句

c# - 将一个表达式作为参数传递到另一个表达式中

c++ - 将函数绑定(bind)到范围以生成迭代函数

javascript - 如何使用 javascript 将 HTML 网页上的字符串替换为另一个字符串?

php - 什么条件处理没有 MySQL 记录与 SQL 语句匹配的事件?