IT

SQL Server의 IsNull () 함수에 해당하는 C #

lottoking 2020. 8. 12. 07:23
반응형

SQL Server의 IsNull () 함수에 해당하는 C #


SQL Server에서 IsNull()함수를 사용하여 값이 null인지 확인하고 null 인 경우 다른 값을 반환 할 수 있습니다. 이제 C #에서 어디에 있는지 궁금합니다.

예를 들어 다음과 같이하고 싶습니다.

myNewValue = IsNull(myValue, new MyValue());

대신에 :

if (myValue == null)
  myValue = new MyValue();
myNewValue = myValue;

감사합니다.


이를 null 병합 ( ??) 연산자 라고합니다 .

myNewValue = myValue ?? new MyValue();

안타깝게도 DBNull과 함께 작동하는 null 병합 연산자에 해당하는 것은 없습니다. 이것을 삼항 연산자를 사용합니다.

newValue = (oldValue is DBNull) ? null : oldValue;

방법을 사용하십시오.

object value2 = null;
Console.WriteLine(object.Equals(value2,null));

public static T isNull<T>(this T v1, T defaultValue)
{
    return v1 == null ? defaultValue : v1;
}

myValue.isNull(new MyValue())

DB Null로 작업하기 위해 VB 애플리케이션을위한 무리를 만들었습니다. VB의 내장 Cxxx 함수와 사용하기 때문에 Cxxx2라고 부릅니다.

내 CLR 확장 프로젝트에서 볼 수 있습니다.

http://www.codeplex.com/ClrExtensions/SourceControl/FileView.aspx?itemId=363867&changeSetId=17967


두 함수를 작성합니다

    //When Expression is Number
    public static double? isNull(double? Expression, double? Value)
    {
        if (Expression ==null)
        {
            return Value;
        }
        else
        {
            return Expression;
        }
    }


    //When Expression is string (Can not send Null value in string Expression
    public static string isEmpty(string Expression, string Value)
    {
        if (Expression == "")
        {
            return Value;
        }
        else
        {
            return Expression;
        }
    }

그들은 아주 잘 작동합니다


내 DataRow 유형에 다음 확장 방법을 사용하고 있습니다.

    public static string ColumnIsNull(this System.Data.DataRow row, string colName, string defaultValue = "")
    {
        string val = defaultValue;
        if (row.Table.Columns.Contains(colName))
        {
            if (row[colName] != DBNull.Value)
            {
                val = row[colName]?.ToString();
            }
        }
        return val;
    }

용법:

MyControl.Text = MyDataTable.Rows[0].ColumnIsNull("MyColumn");
MyOtherControl.Text = MyDataTable.Rows[0].ColumnIsNull("AnotherCol", "Doh! I'm null");

쿼리 결과에 해당 열에 대해 null이 아닌 값이 없으면 DataTable 개체가 해당 열을 제공하지 않기 때문에 먼저 열의 존재를 확인하고 있습니다.


아래 방법을 사용하십시오.

    /// <summary>
    /// Returns replacement value if expression is null
    /// </summary>
    /// <param name="expression"></param>
    /// <param name="replacement"></param>
    /// <returns></returns>
    public static long? IsNull(long? expression, long? replacement)
    {
        if (expression.HasValue)
            return expression;
        else
            return replacement;
    }

    /// <summary>
    /// Returns replacement value if expression is null
    /// </summary>
    /// <param name="expression"></param>
    /// <param name="replacement"></param>
    /// <returns></returns>
    public static string IsNull(string expression, string replacement)
    {
        if (string.IsNullOrWhiteSpace(expression))
            return replacement;
        else
            return expression;
    }

질문이 좀 어리석기 때문에 이것은 농담의 절반을 의미합니다.

public static bool IsNull (this System.Object o)
{
   return (o == null);
}

이것은 확장 메서드이지만 System.Object를 확장하므로 이제 사용하는 모든 개체에 IsNull () 메서드가 있습니다.

그런 다음 다음을 수행하여 수많은 코드를 절약 할 수 있습니다.

if (foo.IsNull())

슈퍼 절름발이 대신 :

if (foo == null)

참고 URL : https://stackoverflow.com/questions/169217/c-sharp-equivalent-of-the-isnull-function-in-sql-server

반응형