我有以下三栏:
根据这一栏,我需要获取数据。
在月份列,数据是:一月,二月,三月.诸若此类。
在科技专栏中,数据是: Gsmnqi,Gsmboi.诸若此类。
在圆圈专栏中,数据是: Ap,Kol,孟买。诸若此类。
我想证明以下4项条件:
但是我的如果条件不起作用的话,我会把这四个条件搞混了。
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,或者我需要检查值是否存在,然后只执行条件。请帮助我理解这段代码。
发布于 2014-07-18 14:27:21
如果你写了一份声明
if(A)
{
// ... foo
}
else if(A && B)
{
// ... bar
}第二个块永远不会被执行。因为如果A是真,那么不管B是什么,第一个块就会被执行。如果A为false,则这两个条件都不为真,因此这两个块都不执行。在这里,您可以做的是切换命令:
if(A && B)
{
// ... bar
}
else if(A)
{
// ... foo
}(其想法是首先测试最严格的条件,只有当它失败时,才测试限制较小的条件)。
然而,在您的情况下,整个逻辑是不必要的复杂,您的代码包含太多的重复。四次重复
query.AppendLine("select * from K2_NQISQI with (nolock) where ...")是一种明目张胆的代码气味。我建议按照以下方针解决这一问题:
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。
发布于 2014-07-18 14:23:33
你看到这部分每一次都重复吗?
"select * from K2_NQISQI with (nolock) where MONTH = '" + nqiSqiEntity.Month 您看到了吗?对于其他列,您只想将它们AND到上面的核心SELECT中吗?
您看到如果nqiSqiEntity.Month是string.Empty,则不能构建有效的查询吗?
然后代码将如下所示:
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");
}下面是编写附件的更好方法。它避免了所有的双引号,单引号,"+",和困难和容易出错的字符串连接。
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列都是灵活的.
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正在进行!
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");https://softwareengineering.stackexchange.com/questions/250358
复制相似问题