我有下面的方法,我需要检查参数是否为空或为空。
public DB Where(string field, string operat, string value, string andOr, string field2, string operat2, string value2)
{
_Where = " WHERE " + field + " " + operat + " @" + field + "1 " + andOr + " " + field2 + " " + operat2 + " @" + field2 + "2 ";
_Params.Add(field + "1", value);
_Params.Add(field2 + "2", value2);
return this;
}
我找到了 string.IsNullOrWhiteSpace 方法,但这需要这么多代码:
if (string.IsNullOrWhiteSpace(field))
throw new ArgumentException("field Cannot be null or be empty");
if (string.IsNullOrWhiteSpace(operat))
throw new ArgumentException("operat Cannot be null or be empty");
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("value Cannot be null or be empty");
if (string.IsNullOrWhiteSpace(andOr))
throw new ArgumentException("andOr Cannot be null or be empty");
if (string.IsNullOrWhiteSpace(field2))
throw new ArgumentException("field2 Cannot be null or be empty");
if (string.IsNullOrWhiteSpace(operat2))
throw new ArgumentException("operat2 Cannot be null or be empty");
if (string.IsNullOrWhiteSpace(value2))
throw new ArgumentException("value2 Cannot be null or be empty");
有什么办法可以缩短这个时间吗?
另外,我尝试为此任务创建一个自定义方法,但是它在自定义方法而不是Where()方法中抛出异常,这使得调试变得棘手。
您可以一一检查该值或创建中间函数来执行此操作。
或者,我的建议是:您可以将所有输入放入一个数组中,并使用 LINQ Any 一次检查所有输入:
public DB Where(string field, string operat, string value, string andOr, string field2, string operat2, string value2)
{
string[] inputs = {field, operat, value, andOr, field2, operat2, value2}
if (inputs.Any(x => string.IsNullOrWhiteSpace(x))){
//throw exception
}
//continue with your method, all inputs are OK
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)