【问题标题】:How to filter empty textboxes with LINQ?如何使用 LINQ 过滤空文本框?
【发布时间】:2020-11-26 19:24:43
【问题描述】:

我的面板中有 6 个文本框,但并非所有文本框都会在每次用户提交时填写。如何使用此 LINQ 方法但过滤掉空文本框?

   public void LogHB()
      {
          var myTextBoxes = HashboardPanel.Controls
          .OfType<TextBox>()
          .Where;
          foreach(TextBox txt in myTextBoxes)
          {
             string hbserial = txt.Text;          
             try
             {
                  SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HMT2DBCS"].ConnectionString);
                  using (SqlCommand command = new SqlCommand("spHBcheckin", connection))
                  {
                       command.CommandType = CommandType.StoredProcedure;
                       command.Parameters.AddWithValue("@order_id", txtorder_id.Text);
                       command.Parameters.AddWithValue("@hbserial", hbserial);
                  }
              }
          }
      }

【问题讨论】:

  • “LINQ 操作”是什么意思?您的代码无法编译 - .Where; 是什么?

标签: c# linq textbox controlcollection


【解决方案1】:
var myTextBoxes = HashboardPanel.Controls
    .OfType<TextBox>()
    .Where(tb => !string.IsNullOrWhitespace(tb.Text));

【讨论】:

  • 谢谢,b.text 中的变量“b”从何而来?那也应该是'tb'吗?
  • @KalenKaczor,对不起,它必须命名为“tb”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
相关资源
最近更新 更多