Hi i would like to query a table based on its primary key.
i have tried both
var deviceDetails = (from d in db.Devices
where d.DeviceId == pointDetails.DeviceId
select d).ToList();
EDIT d.DeviceId
var deviceDetails = db.Devices.Single(d => d.DeviceId == pointDetails.DeviceId)
I know these return different types but this is not the issue right now.
This statement throws an invalidCastException and i dont know why. PointDetails.DeviceId is definitely a valid int. The exception is thrown even if i replace this with a hard coded int.
here is the relevant parts of the stack trace.
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)
Any Help is apreciated as i'm out of ideas.
Class Definition and Schema Here is the Class definition of Device
[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;
Here is the SQL Design
Edit Identified the column causing trouble
I selected one colmun at at time to find the one causing the cast exception and it is DeviceStatusId. What are the constraints on a tinyInt type in SQL? Any suggestions to make this cast correctly
DeviceId
ofnull
in your database, but your model doesn't know it's nullable. – Mike Christensenwhere
clauses are very different. Presumably, at least semantically,d.devices
isn't an integer but rather a collection of some sort. I'm surprised that one even compiles if that's the case. – Daviddb.Devices.Where(d => d.devices == pointDetails.DeviceId);
the Exception is still thrown. – Paperwasteselect d
to justselect d.DeviceId
and I bet it will work - not that the result is any use to you! - but then you'll know for sure. – Stephen Byrne