首页 新闻 会员 周边

为什么long 类型的值给 long类型的数据赋值会报错?

0
悬赏园豆:5 [已解决问题] 解决于 2014-04-22 12:23
 public NarcosisNoteEntity Search(string billId)
        {
            return (from linqEntity in this._Context.NarcosisNote
                where linqEntity.OperationBillID == billId
                select new NarcosisNoteEntity { Id = linqEntity.Id, 
                    OperationBillID = linqEntity.OperationBillID, 
                    NarcosisType =linqEntity.NarcosisType, 
                    NarcosisResult = linqEntity.NarcosisResult, 
                    NarcosisScore = linqEntity.NarcosisScore,
                    NarcosisDoctorId = linqEntity.NarcosisDoctorId, 
                    NarcosisNurseId = linqEntity.NarcosisNurseId,
                    Note = linqEntity.Note, 
                    NotePersonId = linqEntity.NotePersonId, 
                    NoteDate = linqEntity.NoteDate         }).FirstOrDefault<NarcosisNoteEntity>();
        }

 NarcosisType =linqEntity.NarcosisType这段代码时会报错。

IT胡小帅的主页 IT胡小帅 | 初学一级 | 园豆:183
提问于:2014-04-21 17:50
< >
分享
最佳答案
0

有可能一个为long,而另一个为long?,如果是这样可以调用long?.Value = long进行赋值

收获园豆:3
鲜宏 | 初学一级 |园豆:132 | 2014-04-21 18:36

是这样一个情况,我数据库里面是int。但是读出来的时候我需要换成int。但是要怎样转换我不知道。大哥,求解答。

IT胡小帅 | 园豆:183 (初学一级) | 2014-04-21 18:49
[Column(Storage="_NarcosisType", DbType="BigInt NOT NULL")]
        public long NarcosisType
        {
            get
            {
                return this._NarcosisType;
            }
            set
            {
                if (this._NarcosisType != value)
                {
                    this.SendPropertyChanging();
                    this._NarcosisType = value;
                    this.SendPropertyChanged("NarcosisType");
                }
            }
        }

大哥,我这段代码有问题不?

IT胡小帅 | 园豆:183 (初学一级) | 2014-04-21 22:28

@IT白痴: 

有问题,DbType="BigInt 同 long NarcosisType 类型不匹配。

Launcher | 园豆:45045 (高人七级) | 2014-04-22 09:42

@Launcher: 这个我后面自己看到以后改过也不对.

IT胡小帅 | 园豆:183 (初学一级) | 2014-04-22 09:43

@IT白痴: 我的问题是,数据库中 NarcosisType 字段是什么类型?

Launcher | 园豆:45045 (高人七级) | 2014-04-22 09:44

@Launcher: 数据库是Int ,前面我这个DbType="BigInt NOT NULL",后面我该成了DbType="Int NOT NULL"。但是还是不对。

IT胡小帅 | 园豆:183 (初学一级) | 2014-04-22 09:47

@IT白痴: 数据库是 int,那么 DbType = Int,在 C# 中 long 是 64 位,等价于 Int64。因此你的 NarcosisType 应该使用 Int 类型。另外,你的数据库中的 NarcosisType 字段是否允许为空?

Launcher | 园豆:45045 (高人七级) | 2014-04-22 10:00

@Launcher: 数据库中不允许为空。可不可以不改成Int?

IT胡小帅 | 园豆:183 (初学一级) | 2014-04-22 10:02

@IT白痴: 你加个 CanBeNull=false 试试。

Launcher | 园豆:45045 (高人七级) | 2014-04-22 10:12

@Launcher: 好的,我试一下。

IT胡小帅 | 园豆:183 (初学一级) | 2014-04-22 10:15

@Launcher: 对了,大哥。加一个CanBeNull=false就对了。加这句话作用呢,我以前都不会加这个的。

IT胡小帅 | 园豆:183 (初学一级) | 2014-04-22 11:40

@IT白痴: 默认为 CanBeNull=true,表示字段 NarcosisType 是可以为空的类型,而数据库中该字段定义为不能为空,所以类型转换失败。

Launcher | 园豆:45045 (高人七级) | 2014-04-22 13:05
其他回答(2)
0

这两个的类型可能确实不一致~

收获园豆:1
幻天芒 | 园豆:37175 (高人七级) | 2014-04-21 18:33

是这样一个情况,我数据库里面是int。但是读出来的时候我需要换成int。但是要怎样转换我不知道。

大哥,求解答。

支持(0) 反对(0) IT胡小帅 | 园豆:183 (初学一级) | 2014-04-21 18:50

大哥,我这段代码有问题没有?

支持(0) 反对(0) IT胡小帅 | 园豆:183 (初学一级) | 2014-04-21 22:27
0

int转化为long:

  int i=10000;

  long l = new Long(""+i);

别的帮不到你了

收获园豆:1
zl_说不得 | 园豆:33 (初学一级) | 2014-04-21 23:46

嗯嗯,谢了。

支持(0) 反对(0) IT胡小帅 | 园豆:183 (初学一级) | 2014-04-22 00:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册