16

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;
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
user1029184
  • 163
  • 1
  • 1
  • 4

7 Answers7

29

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 B06A6881-003B-4183-A8AB-39B51809F196 someGuid.ToString("N") will return something like B06A6881003B4183A8AB39B51809F196

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: new Guid(customerProfileId).

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;
Marnix van Valen
  • 13,265
  • 4
  • 47
  • 74
5

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:

where C.CustomerId.ToString().Equals(customerProfileId)

but 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.

Community
  • 1
  • 1
ChrisF
  • 134,786
  • 31
  • 255
  • 325
1

You must convert CustomerId to string (calling .ToString()) or customerProfileId to Guid (calling Guid.Parse()) and then compare them.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
0
var accountQuery = from C in CustomerModel.CustomerProfile
              where C.CustomerId == new Guid(customerProfileId) // Error here                    
             select C;

you need to generate new guid from the string and it should work

David Fawzy
  • 1,056
  • 15
  • 17
0

Change it to:

var accountQuery = from C in CustomerModel.CustomerProfile
                  where C.CustomerId.ToString() == customerProfileId                
                 select C;

Or parse your customerProfileId to a Guid and use that in the query.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
0

And what is the question?

Obviously, one of the values is Guid while another one is a string. You can try to compare somehow like this:

C.CustomerId == new Guid(customerProfileId) taking that C.CustomerId is a Guid.

ElDog
  • 1,230
  • 1
  • 10
  • 21
0

Can you do a C.CustomerId.toString() == customerProfileId or replace customerProfileId with new Guid(customerProfileId)

The second one should be faster as its only one conversion and guid compares are faster than string compares.

James Becwar
  • 1,176
  • 2
  • 11
  • 20