title: Specified cast is not valid Linq Query
body: <p>Hi i would like to query a table based on its primary key.</p>
  
  <p>i have tried both</p>
  
  <pre><code>var deviceDetails = (from d in db.Devices
                       where d.DeviceId == pointDetails.DeviceId
                       select d).ToList();  
  </code></pre>
  
  <p><strong>EDIT d.DeviceId</strong></p>
  
  <pre><code>var deviceDetails = db.Devices.Single(d =&gt; d.DeviceId == pointDetails.DeviceId)
  </code></pre>
  
  <p>I know these return different types but this is not the issue right now.</p>
  
  <p>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. </p>
  
  <p>here is the relevant parts of the stack trace.</p>
  
  <pre><code> 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)
  </code></pre>
  
  <p>Any Help is apreciated as i'm out of ideas.</p>
  
  <p><strong>Class Definition and Schema</strong>
  Here is the Class definition of Device</p>
  
  <pre><code>[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;    
  </code></pre>
  
  <p>Here is the SQL Design</p>
  
  <p><img src="https://i.stack.imgur.com/qxhby.png" alt="SQL Design Schema"> </p>
  
  <p><strong>Edit Identified the column causing trouble</strong></p>
  
  <p>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</p>