C# 指定的强制转换不是有效的Linq查询

C# 指定的强制转换不是有效的Linq查询,c#,sql,linq,casting,C#,Sql,Linq,Casting,您好,我想根据表的主键查询表 我两者都试过了 var deviceDetails = (from d in db.Devices where d.DeviceId == pointDetails.DeviceId select d).ToList(); 编辑d.DeviceId 我知道这些返回不同的类型,但这不是现在的问题 这句话引发了一个无效的例外,我不知道为什么。PointDetails.DeviceI

您好,我想根据表的主键查询表

我两者都试过了

var deviceDetails = (from d in db.Devices
                     where d.DeviceId == pointDetails.DeviceId
                     select d).ToList();  
编辑d.DeviceId

我知道这些返回不同的类型,但这不是现在的问题

这句话引发了一个无效的例外,我不知道为什么。PointDetails.DeviceId绝对是有效的int。即使我用硬编码的int替换它,也会引发异常

下面是堆栈跟踪的相关部分

 at System.Data.SqlClient.SqlBuffer.get_Int32()
 at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
 at Read_Device(ObjectMaterializer`1 )
 at        System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
 at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
 at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
我没有主意了,非常感谢你的帮助

类定义和模式 下面是设备的类定义

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Devices")]
public partial class Device : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _DeviceId;
    private int _DeviceTypeId;  
    private string _DeviceName; 
    private string _DeviceMACAddress;   
    private string _DeviceIPAddress;    
    private string _DeviceSubnetMask;
    private string _DeviceGatewayAddress;
    private int _ZoneId;
    private int _TelevisionTypeId;      
    private int _DeviceStatusId;        
    private byte _DeviceIsModified;     
    private int _DeviceSetupBaudRate;       
    private int _DeviceConfigId;        
    private byte _DeviceSetupIsInputInternalPower;      
    private int _DeviceBedSensorInput;      
    private int _DeviceEnsuiteSensorInput;      
    private int _DeviceRoomSensorInput;     
    private string _DeviceSetupString1;     
    private string _DeviceSetupString2;     
    private string _DeviceSetupString3;     
    private string _DeviceSetupString4;     
    private byte _DeviceSetupIsWiegand;     
    private int _DeviceSetupOptionId;       
    private byte _DeviceSetupIsLightMomentary;      
    private string _DeviceTestDateTime;     
    private string _DeviceTestResults;    
下面是SQL设计

编辑确定了导致问题的列

我当时选择了一个colmun来查找导致强制转换异常的colmun,它是DeviceStatusId。
SQL中tinyInt类型的约束是什么?任何正确进行此转换的建议

我认为错误不一定存在于where谓词中,但至少间接存在于select中。数据库中的一列(不一定是DeviceId列)的类型可能与编译的C代码中的属性的类型不同。它可以简单到一个列可以为null,并且在代码属性不可为null的地方包含null值

注意异常发生的位置。这一行表明是在枚举结果时,而不是在调用ToList时计算where子句时:

System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
这条线表明,当一个实例被构造时,或者更确切地说,当一个对象被物化时:

Read_Device(ObjectMaterializer`1 )
与此一样,调用构造函数:

System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)

基本上,至少有一列与代码不匹配,至少有一条记录利用了这种差异。当这种情况发生时,正在构建的对象的构造函数会抛出一个异常,因为它无法理解它正在接收的数据。

看起来数据库中的DeviceId为null,但是你的模型不知道它是可空的。我可以确认没有空设备ID,因为这是表的主键。哪一个会引发异常?where子句是非常不同的。据推测,至少在语义上,d.devices不是一个整数,而是某种类型的集合。“我很惊讶,如果是这样的话,还有人会编译。@David都抛出了相同的异常。如果我使用db.Devices.Whered=>d.Devices==pointDetails.DeviceId;异常仍然被抛出。我猜问题根本不在于主键,而在于您选择的另一列。尝试将select d更改为仅选择d.DeviceId,我打赌它会起作用-结果对您没有任何用处但是你肯定会知道的。你的钱,现在我只需要找到让我悲伤的专栏。我确定专栏是DeviceStatusId。你知道我应该如何修复它吗?@Paperwaste:我想最好的方法是重新生成数据库上下文。这看起来像Linq到Sql,对吗?您应该能够更新DbContext文件,或者删除它并从数据库中重新创建它。在这一点上,代码本身中的任何类型错误都应该成为编译时错误,这更容易识别和更正。private int_DeviceStatusId;->这在数据库架构中指定为tinyint。这可能是问题所在,从byte到int的转换在这个属性上失败了吗?我在dbml文件中重新生成了表,现在查询工作正常了。非常感谢你的帮助斯蒂芬·皮恩和大卫。救生员我今天也学到了不少。
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)