开发者

Simple C# Linq to Entities Query does not work

开发者 https://www.devze.com 2023-03-17 21:36 出处:网络
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 t

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消