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

前端 未结 10 2059
太阳男子
太阳男子 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 21:53

    This is meant half as a joke, since the question is kinda silly.

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

    This is an extension method, however it extends System.Object, so every object you use now has an IsNull() method.

    Then you can save tons of code by doing:

    if (foo.IsNull())
    

    instead of the super lame:

    if (foo == null)
    
    0 讨论(0)
  • 2020-12-12 21:54
        public static T IsNull<T>(this T DefaultValue, T InsteadValue)
        {
    
            object obj="kk";
    
            if((object) DefaultValue == DBNull.Value)
            {
                obj = null;
            }
    
            if (obj==null || DefaultValue==null || DefaultValue.ToString()=="")
            {
                return InsteadValue;
            }
            else
            {
                return DefaultValue;
            }
    
        }
    
    //This method can work with DBNull and null value. This method is question's answer
    
    0 讨论(0)
  • 2020-12-12 21:55

    It's called the null coalescing (??) operator:

    myNewValue = myValue ?? new MyValue();
    
    0 讨论(0)
  • 2020-12-12 21:56

    You Write Two Function

        //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;
            }
        }
    

    They Work Very Well

    0 讨论(0)
  • 2020-12-12 21:57

    Use below methods.

        /// <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;
        }
    
    0 讨论(0)
  • 2020-12-12 22:01

    I've been using the following extension method on my DataRow types:

        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;
        }
    

    usage:

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

    I'm checking for the existence of the column first because if none of query results has a non-null value for that column, the DataTable object won't even provide that column.

    0 讨论(0)
提交回复
热议问题