I'm quite new to Linq,
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.
Ok, I have a table Customers with two columns, CustomerID and CustomerName which belong to a CustomersDataBase
I have successfully been able to return single string from an integer query using the following method (in its basic form):
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;
}
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.
This is my 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;
}
I've also tried thinking SQL terms to make the custName string a char array:
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;
}
The exception disappears but only seem to be returning a "0" for the CustomerID even though I have hundreds of entries.
So in SQL I would just use the following:
Select CustomerID
from Customers
where CustomerName="'"+custName+"'"
Any help would be much appreciated! Thanks
//Edits to code to make it work (following answer from Jon Skeet):-
Ok This works:
[Table(Name = "TableCustomers")] public class Customers {
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public long CustomerID { get; set; }
[Column]
public string CustomerName { get; set; }
[Column]
} //for accomadating for BigInt in SQL Database
public long getCustomerID(string custName) {
CustomerDatabase cdb = new CustomerDatabase();
long custID = (from c in cdb.Customers
where c.CustomerName == custName
select c.CustomerID).SingleOrDefault();
return custID;
}
}