C# equivalent of the IsNull() function in SQL Server

前端 未结 10 2060
太阳男子
太阳男子 2020-12-12 21:53

In SQL Server you can use the IsNull() function to check if a value is null, and if it is, return another value. Now I am wondering if there is anything similar

相关标签:
10条回答
  • 2020-12-12 22:04

    For working with DB Nulls, I created a bunch for my VB applications. I call them Cxxx2 as they are similar to VB's built-in Cxxx functions.

    You can see them in my CLR Extensions project

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

    0 讨论(0)
  • 2020-12-12 22:07

    Sadly, there's no equivalent to the null coalescing operator that works with DBNull; for that, you need to use the ternary operator:

    newValue = (oldValue is DBNull) ? null : oldValue;
    
    0 讨论(0)
  • 2020-12-12 22:11
    public static T isNull<T>(this T v1, T defaultValue)
    {
        return v1 == null ? defaultValue : v1;
    }
    
    myValue.isNull(new MyValue())
    
    0 讨论(0)
  • 2020-12-12 22:18

    Use the Equals method:

    object value2 = null;
    Console.WriteLine(object.Equals(value2,null));
    
    0 讨论(0)
提交回复
热议问题