12

Is there an equivalent of SQL NULLIF function built-in within c#?

Example of what it could look like :

double? result
double denominator = 0;
double Numerator = 100;
result = Numerator / NullIf(denominator, 0);
result = Numerator / denominator.NullIf(0);
result = Numerator / Object.NullIf(denominator, 0);
AXMIM
  • 2,424
  • 1
  • 20
  • 38
  • You meant for the denominator to be nullable and not the result right? – juharr Aug 27 '15 at 15:42
  • I mean to divide by NULL when the value is zero – AXMIM Aug 27 '15 at 15:42
  • 2
    just use ternary operator for result. `result = denominator == 0 ? (double?)null : Numberator/denomiantor;` – vittore Aug 27 '15 at 15:43
  • Also consider that double.NaN behaves much like sql null here, and dosn't require nullables et al. 1 / NaN = NaN etc which is I think what you are looking for, you could easily add your own NaNIf extension function. – tolanj Aug 27 '15 at 15:50
  • Thanks, for pointing that out. However I will stick to NULL since not all type have NaN. For exemple, integer doesn't have NaN. I prefer to use the same pattern every. – AXMIM Aug 27 '15 at 15:56

5 Answers5

9

No, there is no language feature for this at present.

You can easily get the same result, either with a ternary if:

result = Numerator / (denominator == 0 ? (double?)null : denomiantor);

Or even wrapping it as a generic function, so something like:

Nullable<T> NullIf<T>(T left, T right)
{
    return left == right ? (T?)null : left;
}

which could then be called like:

result = Numerator / NullIf(denominator, 0);
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • It works but I really don't like operations on `int?` , `double?` etc. imho `5/null == null` is confusing. – vittore Aug 27 '15 at 15:50
  • 5
    @vittore Seems straight forward to me if you think of `null` as meaning unknown value. 5 divided by an unknown value is an unknown value. – juharr Aug 27 '15 at 16:02
  • Not worth of it's own answer as yours is acceptable, but it could also be an extension method to be more c#'y although it doesn't exactly mirror the SQL variant ` public static class Extensions { public Nullable NullIf(this T left, T right) => left == right ? (T?)null : left; } ... result = numerator / denominator.NullIf(0)` – William Herrmann Aug 03 '21 at 17:17
7

Accepted answer give me:

Operator '==' cannot be applied to operands of type 'T' and 'T'

My working example of NULLIF:

public static T? NullIf<T>(T left, T right) where T : struct
{
    return EqualityComparer<T>.Default.Equals(left, right) ? (T?)null : left;
}
BorisSh
  • 531
  • 4
  • 3
4

No but you could create one.

public static Nullable<T> NullIf<T>(T first, T second) where T : struct
{
    if(first == second)
        return new Nullable<T>();
    return new Nullable<T>(first);
}
juharr
  • 31,741
  • 4
  • 58
  • 93
1

There is no. but you can use ternary operator for compact way of writing it:

double? result
double denominator = 0;
double Numerator = 100;
result = denominator == 0 ? (double?)null : Numerator / denominator;

However there is equivalent of IFNULL:

result = x ?? 0; 

equivalent of:

result = x.HasValue? x.Value : 0;
vittore
  • 17,449
  • 6
  • 44
  • 82
0

Why do we assume this extension method is able to know what is null in our context.

Its the responsibility of the context that calls it. Therefore is a mistake of saying only one value will be considered null as all the response in this thread impliy.

public static T? NullIf<T>(this T value, Func<T,bool> isConsideredNull) 
{
        if(value == null)
        {
           return null;
        }
        return isConsideredNull(value) ? (T?)null : value;
}

We would use it the following way

string test = "NULL";

test.NullIf((x)=> x.Equals("NULL"));
test.NullIf((x)=> x == "NULL");
test.NullIf((x)=> x.Equals("NULL",StringComparison.InvariantCultureIgnoreCase));
JM123
  • 167
  • 1
  • 12