I am getting this error 'Operator '==' cannot be applied to operands of type 'System.Guid' and 'string'' in linq to entityframework below code. in the below code CustomerId is Guid and customerProfileId is string.
var accountQuery = from C in CustomerModel.CustomerProfile where C.CustomerId == customerProfileId // Error here select C;
You cannot compare a Guid to a string directly. Either convert the string to a Guid or the Guid to a string.
Converting a Guid to string is as easy as calling
.ToString() on the variable, but it's important to know that there's more than one way to format the Guid. Either with or without dashes:
someguid.ToString() will give you something like
someGuid.ToString("N") will return something like
If you decide to convert
C.CustomerId to a string make sure you know what format
customerProfileId is in.
If it can be either format, you may be better off converting
customerProfileId to a guid:
The downside of this is that the conversion from string to Guid will throw an exception if it's not formatted correctly. So, if you got the
customerProfileId from user input (like a form field or URL) you should validate it first.
However, if you pull the conversion to Guid outside your query you'll probably end up with better performance since comparing Guids is probably faster than comparing strings.
var customerProfileGuid = new Guid(customerProfileId); // wrap in try catch if needed var accountQuery = from C in CustomerModel.CustomerProfile where C.CustomerId == customerProfileGuid select C;
That's because you can't equate a Guid and a string.
So you need to convert the Guid to string first. Normally I'd suggest:
ToString() doesn't exist in Linq to Entities.
The answer to this question - Problem getting GUID string value in Linq-To-Entity query - will probably help.