首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >混淆在我的如果其他,否则如果条件。在C#中

混淆在我的如果其他,否则如果条件。在C#中
EN

Software Engineering用户
提问于 2014-07-18 13:57:46
回答 2查看 2.2K关注 0票数 -1

我有以下三栏:

  • 月份
  • 技术

根据这一栏,我需要获取数据。

在月份列,数据是:一月,二月,三月.诸若此类。

在科技专栏中,数据是: Gsmnqi,Gsmboi.诸若此类。

在圆圈专栏中,数据是: Ap,Kol,孟买。诸若此类。

我想证明以下4项条件:

  1. 如果我选择月份,它将获取与月份相关的数据,技术和圆圈将不会被选中。
  2. 如果我选择月份和技术,它将获取与月份和技术相关的数据,不会选择圆圈。
  3. 如果我选择月份和圆圈,它将获取与月和圆相关的数据,技术将不会被选中。
  4. 如果我选择月份,技术和圆环,它将获取与月份,技术和圆相关的数据。

但是我的如果条件不起作用的话,我会把这四个条件搞混了。

代码语言:javascript
复制
  if (nqiSqiEntity.Month != string.Empty)
  {
      query.AppendLine("select * from K2_NQISQI with (nolock) where MONTH = '" + nqiSqiEntity.Month + "'  order by id asc");
  }
  else if (nqiSqiEntity.Month != string.Empty && nqiSqiEntity.Tech != string.Empty)
  {
      query.AppendLine("select * from K2_NQISQI with (nolock) where MONTH = '" + nqiSqiEntity.Month + "' and TECH = '" + nqiSqiEntity.Tech + "' order by id asc");
  }
  else if (nqiSqiEntity.Month != string.Empty && nqiSqiEntity.Circle != string.Empty)
  {
      query.AppendLine("select * from K2_NQISQI with (nolock) where MONTH = '" + nqiSqiEntity.Month + "' and CIRCLE = '" + nqiSqiEntity.Circle + "' order by id asc");
  }
  else
  {
      query.AppendLine("select * from K2_NQISQI with (nolock) where MONTH = '" + nqiSqiEntity.Month + "' and CIRCLE = '" + nqiSqiEntity.Circle + "' and TECH '" + nqiSqiEntity.Tech + "' order by id asc");
  }

在条件下,我需要放置值而不是string.Empty,或者我需要检查值是否存在,然后只执行条件。请帮助我理解这段代码。

EN

回答 2

Software Engineering用户

发布于 2014-07-18 14:27:21

如果你写了一份声明

代码语言:javascript
复制
if(A)
{
    // ... foo
}
else if(A && B)
{
    // ... bar
}

第二个块永远不会被执行。因为如果A是真,那么不管B是什么,第一个块就会被执行。如果A为false,则这两个条件都不为真,因此这两个块都不执行。在这里,您可以做的是切换命令:

代码语言:javascript
复制
if(A && B)
{
    // ... bar
}
else if(A)
{
    // ... foo
}

(其想法是首先测试最严格的条件,只有当它失败时,才测试限制较小的条件)。

然而,在您的情况下,整个逻辑是不必要的复杂,您的代码包含太多的重复。四次重复

代码语言:javascript
复制
  query.AppendLine("select * from K2_NQISQI with (nolock) where ...")

是一种明目张胆的代码气味。我建议按照以下方针解决这一问题:

代码语言:javascript
复制
 var conditions = new List<string>();
 if (nqiSqiEntity.Month != string.Empty)
     conditions.Add("MONTH = '" + nqiSqiEntity.Month+"'");
 if(nqiSqiEntity.Tech != string.Empty)
     conditions.Add("TECH = '" + nqiSqiEntity.Tech+"'");
 if(nqiSqiEntity.Circle != string.Empty)
     conditions.Add("CIRCLE = '" + nqiSqiEntity.Circle + "'");
 if(conditions.Count>0)
 {
      string finalCondition= "select * from K2_NQISQI with (nolock) WHERE " + 
                            string.Join(" and ", conditions.ToArray())
                            + " order by id asc";
      query.AppendLine(finalCondition);
 }

请注意,这是“空中代码”,它没有处理这样一个事实:在您的问题中,创建条件总是需要nqiSqiEntity.Month

票数 6
EN

Software Engineering用户

发布于 2014-07-18 14:23:33

你看到这部分每一次都重复吗?

代码语言:javascript
复制
"select * from K2_NQISQI with (nolock) where MONTH = '"  + nqiSqiEntity.Month 

您看到了吗?对于其他列,您只想将它们AND到上面的核心SELECT中吗?

您看到如果nqiSqiEntity.Monthstring.Empty,则不能构建有效的查询吗?

然后代码将如下所示:

代码语言:javascript
复制
  if (nqiSqiEntity.Month != string.Empty)
  {
      query.AppendLine("select * from K2_NQISQI with (nolock) where MONTH = '" + nqiSqiEntity.Month + "'");

      if (nqiSqiEntity.Tech != string.Empty)
      {
          query.AppendLine("' and TECH = '" + nqiSqiEntity.Tech + "'");
      }

      if (nqiSqiEntity.Circle != string.Empty)
      {
          query.AppendLine("' and CIRCLE = '" + nqiSqiEntity.Circle ");
      }

      query.AppendLine( "'  order by id asc");
  }

下面是编写附件的更好方法。它避免了所有的双引号,单引号,"+",和困难和容易出错的字符串连接。

代码语言:javascript
复制
  if (nqiSqiEntity.Month != string.Empty)
  {
      query.AppendFormat("select * from K2_NQISQI with (nolock) where MONTH = '{0}'",nqiSqiEntity.Month);
      if (nqiSqiEntity.Tech != string.Empty)
      {
          query.AppendFormat("' and TECH = '{0}'", nqiSqiEntity.Tech);
      }

      if (nqiSqiEntity.Circle != string.Empty)
      {
          query.AppendFormat("' and CIRCLE = '{0}'", nqiSqiEntity.Circle);
      }

      query.AppendLine( " order by id asc");
  }

哦,我想你希望这个查询对所有3列都是灵活的.

代码语言:javascript
复制
  query.AppendLine("select * from K2_NQISQI with (nolock)");

  // if at least 1 of the columns is not empty add a where clause
  if (! (nqiSqiEntity.Month != string.Empty &&
      nqiSqiEntity.Tech != string.Empty  &&
      nqiSqiEntity.Circle != string.Empty)) {

      query.AppendLine (" where ");  // we are going to add on something

      if (nqiSqiEntity.Month != string.Empty)
      {
          query.AppendFormat(" MONTH = '{0}'",nqiSqiEntity.Month);
      }

      if (nqiSqiEntity.Tech != string.Empty)
      {
          query.AppendFormat("' and TECH = '{0}'", nqiSqiEntity.Tech);
      }

      if (nqiSqiEntity.Circle != string.Empty)
      {
          query.AppendFormat("' and CIRCLE = '{0}'", nqiSqiEntity.Circle);
      }
  }

  query.AppendLine( " order by id asc");

这事还是搞砸了。因此,也许下面的内容不会那么令人困惑,但仍然会有大量的if正在进行!

最终答案

代码语言:javascript
复制
  query.AppendLine("select * from K2_NQISQI with (nolock)");

  // if at least 1 of the columns is not empty add a where clause
  if (! (nqiSqiEntity.Month != string.Empty &&
      nqiSqiEntity.Tech != string.Empty  &&
      nqiSqiEntity.Circle != string.Empty)) {

      query.AppendLine (" where ");  // we are going to add on something

      if (nqiSqiEntity.Month != string.Empty)
      {
          query.AppendFormat(" MONTH = '{0}'",nqiSqiEntity.Month);

          // will there be any "and"ing ?
          if (nqiSqiEntity.Tech != string.Empty || nqiSqiEntity.Circle != string.Empty) 
              query.AppendLine(" and " );
      }

      if (nqiSqiEntity.Tech != string.Empty)
      {
          query.AppendFormat(" TECH = '{0}'", nqiSqiEntity.Tech);

          if (nqiSqiEntity.Circle != string.Empty)
              query.AppendLine(" and " );
      }

      if (nqiSqiEntity.Circle != string.Empty)
      {
          query.AppendFormat(" CIRCLE = '{0}'", nqiSqiEntity.Circle);
      }
  }

  query.AppendLine( " order by id asc");
票数 0
EN
页面原文内容由Software Engineering提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://softwareengineering.stackexchange.com/questions/250358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档