繁体   English   中英

将int转换为nullable int?

[英]convert int to nullable int?

我需要知道如何将int转换为可以为null的int。 但是,我不断收到错误“没有为类型'System.Nullable`1 [System.Int32]'和'System.Int32'定义二进制运算符Equal。” 任何解决方案 它需要是Microsoft SQL Server的可空int类型。

 somevalue = Expression.Constant(something.GetValue(some,null).To<Nullable<System.Int32>> ());

public static T To<T>(this object obj)
    {
        Type t = typeof(T);
        Type u = Nullable.GetUnderlyingType(t);

        if (u != null)
        {
            if (obj == null)
                return default(T);

            return (T)Convert.ChangeType(obj, u);
        }
        else
        {
            return (T)Convert.ChangeType(obj, t);
        }
    }'

To代码似乎您试图建构一个Constant可空类型的给定的非可空类型的值时,但不是在所有正确的方式去了解这一点 您尝试这样做的方式表明您对盒装值类型的工作方式存在误解。

该错误消息表明您正在构造一个二元运算符表达式树节点,该节点的操作数为nullable int类型的表达式节点和int类型的表达式节点。 那不合法; 它们必须都是可以空的。 您应该做的是将非可空int表达式树节点包装在Convert 表达式树节点中 ,该节点将其转换为可为空的int,然后将传递给二元运算符表达式树节点构造函数。

也就是说,这是错误的:

var someIntExpr = Expression.Constant(123, typeof(int));
var someNubIntExpr = Expression.Constant(null, typeof(int?));
var badEq = Expression.Equal(someIntExpr, someNubIntExpr);

这是对的:

var goodEq = Expression.Equal(Expression.Convert(someIntExpr, typeof(int?)),  someNubIntExpr);

那你为什么做错了?

你有一个返回T的方法To<T> 它正确地接受一个int并返回等效的int? 那么什么呢? 你将它传递给Expression.Constant ,它将可以为空的int 打包成一个盒装的int ,然后从中取出一个常量。 您认为存在盒装可空值类型这样的东西,但没有! 可空值的类型框可以是空引用,也可以是盒装非可空值类型。

所以你也可以通过不做任何这些疯狂的事情来解决你的问题。 如果你手头有一个盒装的int,并且你需要一个可空类型的常量表达式树节点, 只需提供类型

Expression.Constant(someBoxedIntValue, typeof(int?))

完成。 所以:结束,你有两个解决方案:

  • 如果您手头有一个盒装int,请将它和您想要的可空值类型传递给Constant工厂,或者
  • 如果你手头有一个int类型的表达式节点,那么使用Convert表达式节点工厂,并将它和所需的类型传递给它。

两者都会返回一个正确类型的表达式节点,以便与另一个可空的int进行比较。

通常,您将int转换为int? 使用演员。

int? myNullable = (int?) 15;
int myInt = (int) myNullable;
int test = 0; // set int

int? num = test; // convert test to a nullable int

num = null; // set num as null
int i = 1;
int? k;
k = i as int?;

像这样你将i转换为一个int为可空的int;)

int? Nullable<int>的简短版本。

像这样简单的事情不起作用吗?

int i; 
int? temp = int.TryParse(<your value>, out i) ? (int?)i : null;

干得好。 通用字符串到可空的原始解决方案。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM