I need to know how to convert an int to a nullable int. However, I keep getting an error \"The binary operator Equal is not defined for the types \'System.Nullable`1[System.
Does something simpler like this not work?
int i;
int? temp = int.TryParse(<your value>, out i) ? (int?)i : null;
int i = 1;
int? k;
k = i as int?;
Like this you will convert i
which is an int to a nullable int ;)
int?
is the short version of Nullable<int>
.
int test = 0; // set int
int? num = test; // convert test to a nullable int
num = null; // set num as null
Here you go. A generic string to nullable primitive solution.
int? n = " 99 ".ToNullable<int>();
/// <summary>
/// Developed by Taylor Love
/// </summary>
public static class ToNullableStringExtension
{
/// <summary>
/// <para>More convenient than using T.TryParse(string, out T).
/// Works with primitive types, structs, and enums.
/// Tries to parse the string to an instance of the type specified.
/// If the input cannot be parsed, null will be returned.
/// </para>
/// <para>
/// If the value of the caller is null, null will be returned.
/// So if you have "string s = null;" and then you try "s.ToNullable...",
/// null will be returned. No null exception will be thrown.
/// </para>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="p_self"></param>
/// <returns></returns>
public static T? ToNullable<T>(this string p_self) where T : struct
{
if (!string.IsNullOrEmpty(p_self))
{
var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
if (converter.IsValid(p_self)) return (T)converter.ConvertFromString(p_self);
if (typeof(T).IsEnum) { T t; if (Enum.TryParse<T>(p_self, out t)) return t;}
}
return null;
}
https://github.com/Pangamma/PangammaUtilities-CSharp/tree/master/src/StringExtensions
Typically, you convert an int
an int?
using a cast.
int? myNullable = (int?) 15;
int myInt = (int) myNullable;
That To
code seems to be you trying to construct a Constant
of nullable type when given a value of non-nullable type but that is not at all the right way to go about this. The way you're trying to do this indicates that you have a misunderstanding about how boxed value types work.
That error message indicates that you are constructing a binary operator expression tree node which has as its operands an expression node of nullable int type and an expression node of int type. That's not legal; they have to be both nullable int. What you should be doing is wrapping the non-nullable int expression tree node in a Convert
expression tree node which converts it to a nullable int, and then pass that to the binary operator expression tree node constructor.
That is, this is wrong:
var someIntExpr = Expression.Constant(123, typeof(int));
var someNubIntExpr = Expression.Constant(null, typeof(int?));
var badEq = Expression.Equal(someIntExpr, someNubIntExpr);
This is right:
var goodEq = Expression.Equal(Expression.Convert(someIntExpr, typeof(int?)), someNubIntExpr);
So why is what you're doing wrong?
You have a method To<T>
which returns a T
. It correctly takes in an int
and returns the equivalent int?
. So then what? You pass that to Expression.Constant
, which boxes the nullable int into a boxed int, and then makes a constant out of that. You believe that there is such a thing as a boxed nullable value type, but there is not! A nullable value type boxes either to a null reference or to a boxed non-nullable value type.
So you could also solve your problem by not doing any of this crazy stuff in the first place. If you have a boxed int in hand, and you need a constant expression tree node of nullable type, just provide the type.
Expression.Constant(someBoxedIntValue, typeof(int?))
Done. So: wrapping up, you have two solutions:
Constant
factory, orConvert
expression node factory, and pass it and the desired type to that.Both will give you back an expression node of the correct type to be compared to another nullable int.