Simple C# Linq to Entities Query does not work
I keep getting an error with the code below. It says Error "Operator '==' cannot be applied to operands of type 'string' and 'int'.
I have watched a video and Julie Lerman does the exact same thing. She doesn't get the error though. Why?
private static void CustomerQuery()
{
var context = new NorthwindEntities();
var query开发者_StackOverflow中文版 = from c in context.Customers
where c.CustomerID == 5
select c;
var customers = query.FirstOrDefault();
}
Check the data type of CustomerID. Else convert them to int.
thats it,check the type of customer id in table ,if it is string change the query to
private static void CustomerQuery()
{
var context = new NorthwindEntities();
var query = from c in context.Customers
where c.CustomerID == "5"
select c;
var customers = query.FirstOrDefault();
}
try with where c.CustomerID == "5"
instead.
It seems that CustomerID
is not of type int
. Typecast
it to int
to compare it with an int datatype
.
精彩评论