Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查多个参数的null或空字符串C#_C#_String_Null_Isnullorempty - Fatal编程技术网

如何检查多个参数的null或空字符串C#

如何检查多个参数的null或空字符串C#,c#,string,null,isnullorempty,C#,String,Null,Isnullorempty,我有下面的方法,我需要检查参数是空的还是空的 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 + "

我有下面的方法,我需要检查参数是空的还是空的

    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()方法,这使得调试变得很棘手

我的建议是:

private string _nullChecker(string _value){
   if (string.IsNullOrWhiteSpace(_value))
            throw new ArgumentException(_value + "Cannot be null or be   empty");
   return _value;
}
然后,在Where字符串语句中

_Where = " WHERE " + _nullChecker(field) + " " + __nullChecker(operat) + " @" + _nullChecker(field) + "1 " + _nullChecker(andOr) + " " + _nullChecker(field2) + " " + _nullChecker(operat2) + " @" + _nullChecker(field2) + "2 ";

不过我不确定。未使用实际代码进行检查。:)希望这对您有所帮助

您可以逐个检查值或创建中间函数来执行此操作

或者,我的建议是:您可以将所有输入放在一个数组中,并使用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
}
您可以这样做:

int? GetLength(string s) {
    return s == "" ? -1 : s?.Length;
}

// s1, s2 and so on are your parameters
int? lengthSum = GetLength(s1) + GetLength(s2); // and so on
int wholeLength = (s1 + s2).Length; // and so on
if(lengthSum == wholeLength) {
    // No parameter is null or empty
}

首先,可以使用简单的库进行参数验证。看看这个名为的,它有方便的函数,可以将您的总体代码减少一半

下面是一个关于如何使用参数验证程序库执行此操作的示例:

public DB Where(string field, string operat, string value, string andOr, string field2, string operat2, string value2)
{
    var inputs = new string[] {field, operat, value, andOr, field2, operat2, value2};
    foreach(var input in inputs)
    {
        Throw.IfNullOrEmpty(input, nameof(input)));
    }
}    

您可以创建一个静态方法:
ValidateParameterNotEmpty(字符串名称、字符串值)
。您将把行数减少到1/3。为防止SQL注入,您需要进行的验证将要做更多的工作,因此我将把此列为第二优先事项。如果字段为null或空,则在按您的方式抛出
ArgumentException
时将不会显示任何内容。你想使用吗?我确实尝试过类似的方法,但发现当它抛出异常时,调试器转到ValidateParameterNotEmpty()方法,而不是Where()方法。当然,如果我在多个地方使用该方法,可能会使调试变得棘手@xanatos@GediminasMasaitis是的,对不起,那是个错误。虽然我不相信我可以使用nameof(),因为我没有使用c#6。我会将它修改为我现在拥有的。或者它可能是一个扩展方法。