INT转换成可空INT? [英] convert int to nullable int?

查看:162
本文介绍了INT转换成可空INT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何为int转换为可空int类型。不过,我不断收到一个错误二进制运算符等于没有为类型System.Nullable`1 [System.Int32]'和'System.Int32定义。任何解决方案。它需要的Microsoft SQL Server的可为空的整型。

  someValue中=前pression.Constant(something.GetValue(一些,空)。为了<可空< System.Int32>>());公共静态ŧ到< T>(此对象OBJ)
    {
        类型T = typeof运算(T);
        U型= Nullable.GetUnderlyingType(T);        如果(U!= NULL)
        {
            如果(OBJ == NULL)
                返回默认(T);            回报(T)Convert.ChangeType(OBJ,U);
        }
        其他
        {
            回报(T)Convert.ChangeType(OBJ,T);
        }
    }


解决方案

这是 code似乎你试图构建一个鉴于非可空类型的值时,但那是的不是在所有正确的方式去了解这个可空类型的。你想做到这一点的方式表明您对类型是如何装箱值工作的误解。

这是错误信息表明,你正在构建一个二元操作前pression树,作为其操作数为空的int型的前pression节点和int型的前pression节点的节点。这是不合法的;他们要的两个的可空int类型。什么,你应该做的是包装的非可空INT前pression树在转换 前pression树节点哪个节点将其转换为可空类型为int,然后通过的的二进制运算符前pression树节点构造。

这是,这是错误的:

  VAR someIntExpr =前pression.Constant(123的typeof(INT));
VAR someNubIntExpr =前pression.Constant(空的typeof(INT));
VAR badEq =前pression.Equal(someIntExpr,someNubIntExpr);

这是正确的:

  VAR goodEq =前pression.Equal(前pression.Convert(someIntExpr的typeof(INT)),someNubIntExpr?);

那么,为什么你在做什么错了?

您有的方法到< T> 返回一个 T 。它正确地发生在一个 INT 并返回相当于 INT?。那么又是什么?你,传递给防爆pression.Constant ,这盒可空INT 到装箱的int ,然后做一个恒定出来的。你相信有这样的事,作为一个盒装空值类型,但是没有!可空值类型框中要么空引用或盒装非空值类型。

所以,你也可以通过没有做任何的这个疯狂的东西摆在首位解决您的问题。如果你手上装箱的int,你需要可空类型,只是提供类型的常量前pression树节点

 防爆pression.Constant(someBoxedIntValue的typeof(INT?))

完成。所以:结束了,你有两种解决方法:


  • 如果你手上装箱的int,通过它,你想在的工厂,或者
  • 的空值类型
  • 如果您手中有int类型的前pression节点然后使用转换前pression节点工厂,并通过它与所需的类型了这一点。

双方会给你回一个前$正确类型的对$ pssion节点被比作另一个可空int类型。

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.Int32]' and 'System.Int32'." Any solution. It needs to be Microsoft SQL Server's nullable int type.

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

解决方案

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:

  • If you have a boxed int in hand, pass it and the nullable value type you want to the Constant factory, or
  • if you have an expression node of type int in hand then use the Convert 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.

这篇关于INT转换成可空INT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆