Alex Alex - 9 months ago 33
C# Question

== operator cannot be applied of type int and string -- LINQ statement with ==

I am writing a LINQ statement like this

var v1 = from c in context.Orders
join c1 in context.Order_Details
on c.OrderID equals c1.OrderID
where c.CustomerID == 1
group c1 by c1.ProductID into x
select x;

But its giving me error that

== cannot be applied to operands of type int and string

There is following question Comparison operators not supported for type 'System.Linq.IQueryable`1[System.Int32]'
But this question does not solve my problem

How can I fix the above problem, thanks


So, I take it that c.CustomerID is a string (as is common with ID properties), and that's the problem. As is pointed out above, this isn't a Linq problem.

That being said, to solve it, just replace

where c.CustomerID == 1


where c.CustomerID == "1"

If you have an int variable and not a constant, you can just call ToString() on it:

where c.CustomerID == myInt.ToString()

This won't work for doubles and floats though, so watch out!