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

前端 未结 10 2067
太阳男子
太阳男子 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:54

        public static T IsNull(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
    

提交回复
热议问题