C# 等效于 SQL Server 中的 IsNull() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/169217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:29:16  来源:igfitidea点击:

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

c#.netsql-serverisnull

提问by HAdes

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 in C#.

在 SQL Server 中,您可以使用该IsNull()函数检查一个值是否为空,如果是,则返回另一个值。现在我想知道 C# 中是否有类似的东西。

For example, I want to do something like:

例如,我想做这样的事情:

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

instead of:

代替:

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

Thanks.

谢谢。

采纳答案by Kent Boogaart

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

它被称为空合并 ( ??) 运算符:

myNewValue = myValue ?? new MyValue();

回答by Robert Rossney

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

可悲的是,没有与 DBNull 一起使用的空合并运算符等价物;为此,您需要使用三元运算符:

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

回答by Jonathan Allen

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.

为了处理 DB Nulls,我为我的 VB 应用程序创建了一堆。我称它们为 Cxxx2,因为它们类似于 VB 的内置 Cxxx 函数。

You can see them in my CLR Extensions project

您可以在我的 CLR 扩展项目中看到它们

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

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

回答by FlySwat

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.

这是一个扩展方法,但它扩展了 System.Object,因此您现在使用的每个对象都有一个 IsNull() 方法。

Then you can save tons of code by doing:

然后您可以通过执行以下操作来节省大量代码:

if (foo.IsNull())

instead of the super lame:

而不是超级跛脚:

if (foo == null)

回答by serializer

Use the Equals method:

使用 Equals 方法:

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

回答by Rudy

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

myValue.isNull(new MyValue())

回答by Mansoor Bozorgmehr

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

他们工作得很好

回答by Denis M. Kitchen

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

我一直在我的 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;
    }

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.

我首先检查该列是否存在,因为如果没有查询结果具有该列的非空值,则 DataTable 对象甚至不会提供该列。

回答by sushil suthar

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

回答by Mansoor Bozorgmehr

    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