Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异常:无法将null值分配给System.Int32类型的成员,该类型是不可为null的值类型_C#_Linq_Linq To Sql - Fatal编程技术网

C# 异常:无法将null值分配给System.Int32类型的成员,该类型是不可为null的值类型

C# 异常:无法将null值分配给System.Int32类型的成员,该类型是不可为null的值类型,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,有人可以解释为什么在以下LINQ查询中发生此异常: return (from c in dc.Classifications where c.Id == classificationId select new Classification() { Description = c.Description, P

有人可以解释为什么在以下LINQ查询中发生此异常:

        return (from c in dc.Classifications
                where c.Id == classificationId
                select new Classification()
                {
                    Description = c.Description,
                    ParentId = Convert.ToInt16(c.ParentId),
                }).Single<Classification>();
返回(来自dc分类中的c
其中c.Id==classificationId
选择新分类()
{
描述=c.描述,
ParentId=Convert.ToInt16(c.ParentId),
}).Single();
dc是datacontext,Classification是包含int属性ParentId的类。ParentId列是Sql Server数据库中可为空的int。如果数据库中的ParentId字段为null,则该语句返回InvalidOperationException

换句话说,为什么上面的查询失败了 'int y=Convert.ToInt16(null);'

工作?

假设当0在数据库中为空时,您希望将其作为父ID:

    return (from c in dc.Classifications
            where c.Id == classificationId
            select new Classification()
            {
                Description = c.Description,
                ParentId = Convert.ToInt16(c.ParentId ?? 0),
            }).Single<Classification>();
返回(来自dc分类中的c
其中c.Id==classificationId
选择新分类()
{
描述=c.描述,
ParentId=Convert.ToInt16(c.ParentId±0),
}).Single();
我的回答还假设,
classification.ParentId
的类型为
null
/
int?
,如果数据库中的列为null,则其值为
null


我唯一改变的是
??0
转换中的零件。如果
c.ParentId
null
则使用
0
,否则使用
c.ParentId
。有关更多信息,请参阅。

c.ParentId的类型为
int?

您对Convert.ToInt16的调用将以两种方式中断;如果ParentId大于short.MaxValue或小于short.MinValue,则会失败


为什么那个电话在那里?移除它。另外,将
Single()
替换为
SingleOrDefault()
,以便它可以在适当的时候返回null。

如果
c.PartentId
可以是
null
,则
Convert.ToInt16(null)
将引发异常

既然您指出
Classification.ParentId
是一个int,那么您使用它的原因是什么
Convert.ToInt16
改为短码?你不想改成32岁吗?既然如此,为什么要皈依呢?简单地说:

ParentId = c.ParentId ?? 0
…只是吹毛求疵,从技术上讲,您不需要在Linq表达式的末尾指定类型:

.Single<Classification>()

更新:

哦,我明白了,很抱歉我看错了你原来的问题。问题是为什么:

int y = Convert.ToInt16(null);
工作,而Linq2Sql表达式中的相同内容会引发异常

我没有很好的答案,只是要指出,虽然表达式在代码中看起来是相同的,但它们实际上是由两个不同的Linq实现处理的。(与接口相同,接口可以有不同的支持实现)

在下列情况下:

int y = Convert.ToInt16(null);
您正在直接呼叫Convert.ToInt16。这似乎将null转换为
默认值
,其中T是所需的类型(因此在本例中返回0)

但是,在Linq2Sql表达式中使用时,表达式及其投影将交给Linq2Entities或Linq2Sql处理。那东西里面可能有个虫子。用作基本的LINQ2对象(或任何您想称之为LINQ2对象的东西),它实际上似乎工作正常:

[TestMethod] // test passes
public void TestLinqToObjects()
{
  var stuff = new List<int?>() { null };
  var y = (from x in stuff
           select Convert.ToInt32(x))
           .First();
  Assert.AreEqual(0, y);
}
[TestMethod]//测试通过
public void TestLinqToObjects()
{
var stuff=new List(){null};
变量y=(从材料中的x开始)
选择Convert.ToInt32(x))
.First();
断言.AreEqual(0,y);
}
上面的“select”可以工作,但是,将相同的Linq表达式放在Linq2Sql或EntityFramework集合上会导致Linq处理器的不同实现来处理该表达式,并且它可能会做一些不同的事情来尝试选择该表达式,或者将其中一些转换为SQL语句,或者可能只是有其他实现没有的bug


我知道这并不能真正解决您的问题,但它可能有助于解释这一问题?

当数据库中的字段为空时,您希望ParentID中有什么值?int x=Convert.ToInt16(matches.Single().ParentID);当数据库中的ParentId为null时,这不会引发任何异常吗?…奇怪!!!如果数据库字段为空,我希望ParentId为零。我的解决方案对您没有帮助吗?还请发布该
InvalidOperationException
@Daniel Hilgarth的确切错误消息和堆栈跟踪:是的,它确实存在,我已将代码更改为使用c.ParentId??0但是,我还想知道为什么转换不能在Linq2Sql中工作。抱歉,不清楚。可空x=null;int y=转换为16(x);结果y=0。当它是LINQ表达式的一部分时,为什么会导致异常??Nullable x=null;int y=转换为16(x);结果y=0。当它是LINQ表达式的一部分时,为什么会导致异常??
[TestMethod] // test passes
public void TestLinqToObjects()
{
  var stuff = new List<int?>() { null };
  var y = (from x in stuff
           select Convert.ToInt32(x))
           .First();
  Assert.AreEqual(0, y);
}