你好,我想根据主键查询表格。
我已经尝试了两种方法:
编辑 d.DeviceId
我知道这些返回不同的类型,但现在不是问题所在。
这个语句会抛出一个 InvalidCastException 异常,我不知道为什么。PointDetails.DeviceId 明显是一个有效的 int。即使我用一个硬编码的 int 替换它,异常也会被抛出。
以下是堆栈跟踪的相关部分。
任何帮助都会被欣然接受,因为我已经没有想法了。
这里是SQL设计。
编辑已经确认了引起问题的列。
我逐个选择列以查找引起转换异常的列,发现是DeviceStatusId。在SQL中tinyInt类型的约束是什么?有没有建议可以使转换正确?
我已经尝试了两种方法:
var deviceDetails = (from d in db.Devices
where d.DeviceId == pointDetails.DeviceId
select d).ToList();
编辑 d.DeviceId
var deviceDetails = db.Devices.Single(d => d.DeviceId == pointDetails.DeviceId)
我知道这些返回不同的类型,但现在不是问题所在。
这个语句会抛出一个 InvalidCastException 异常,我不知道为什么。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设计。
编辑已经确认了引起问题的列。
我逐个选择列以查找引起转换异常的列,发现是DeviceStatusId。在SQL中tinyInt类型的约束是什么?有没有建议可以使转换正确?
where
子句非常不同。可以推测,至少在语义上,d.devices
不是整数,而是某种集合。如果是这样的话,我很惊讶它甚至能够编译通过。 - Daviddb.Devices.Where(d => d.devices == pointDetails.DeviceId);
,仍然会抛出异常。 - Paperwasteselect d
改为select d.DeviceId
,我敢打赌它会起作用 - 虽然结果对你没有任何用处!- 但这样你就可以确定了。 - Stephen Byrne