title: Linq returning single integer value based on string stored in SQL
body: <p>I'm quite new to Linq,</p>
  
  <p>I'm just trying to do a routine return which is usually done in SQL and cannot seem to get a straight answer from viewing many forums with hours of browsing.</p>
  
  <p>Ok, I have a table Customers with two columns, CustomerID and CustomerName which belong to a CustomersDataBase</p>
  
  <p>I have successfully been able to return single string from an integer query using the following method (in its basic form):</p>
  
  <pre><code>public string getCustomerName(int custID)
  {
      CustomerDataBase cdb = new CustomerDataBase();
  
      string custName = (
          from c in cdb.Customers
          where c.CustomerID == custID
          select c.CustomerName)
          .SingleOrDefault();
  
      return custName;
  }
  </code></pre>
  
  <p>Ok that's fine but when I try to inverse the situation with trying to return the CustomerID it returns an Exception: System.InvalidCastException: Specified cast is not valid.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public int getCustomerID(string custName)
  {
      CustomerDataBase cdb = new CustomerDataBase();
  
      int custID = (
          from c in cdb.Customers
          where c.CustomerName == custName
          select c.CustomerID)
          .SingleOrDefault();
  
      return custID;
  }
  </code></pre>
  
  <p>I've also tried thinking SQL terms to make the custName string a char array:</p>
  
  <pre><code>public int getCustomerID(string custName)
  { 
      CustomerDataBase cdb = new CustomerDataBase();
  
      int custID = (
          from c in cdb.Customers
          where c.CustomerName == "'"+custName+"'"
          select c.CustomerID)
          .SingleOrDefault();
  
      return custID;
  }
  </code></pre>
  
  <p>The exception disappears but only seem to be returning a "0" for the CustomerID even though I have hundreds of entries.</p>
  
  <p>So in SQL I would just use the following:</p>
  
  <pre><code>Select CustomerID 
  from   Customers 
  where  CustomerName="'"+custName+"'"
  </code></pre>
  
  <p>Any help would be much appreciated! Thanks</p>
  
  <p>//Edits to code to make it work (following answer from Jon Skeet):-</p>
  
  <p>Ok This works:</p>
  
  <p>[Table(Name = "TableCustomers")]
      public class Customers
      {</p>
  
  <pre><code>    [Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public long CustomerID { get; set; }
      [Column]
      public string CustomerName { get; set; }
      [Column]
  </code></pre>
  
  <p>}
  //for accomadating for BigInt in SQL Database</p>
  
  <p>public long getCustomerID(string custName)
          {</p>
  
  <pre><code>        CustomerDatabase cdb = new CustomerDatabase();
  
  
  
             long custID = (from c in cdb.Customers
  
                            where c.CustomerName == custName
                            select c.CustomerID).SingleOrDefault();
  
          return custID;
      }
  }
  </code></pre>