Link to home
Start Free TrialLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

Operator '&&' cannot be applied to operands of type 'System.Guid' and 'int?

I am trying to build a LINQ query using a GUID in a where clause and keep getting this error message.  Why can you not search using a GUID as a key?

Here is my code.
var patientlookup = from MPIREC in db.MPIs
                                                    where recordMPI.MPI1 = initGuid &&------squiggle under initGuid
                                                    recordMPI.NPI = ecDoctor.NPI
                                                    select new { MPIREC };

Open in new window

Avatar of naspinski
naspinski
Flag of United States of America image

I am assuming that recordMPI.MPI1 is an int, and initGuid is a GUID.  They are not the same kind of variable, so you cannot compare them.  You will have to convert one to the other before you compare.
ASKER CERTIFIED SOLUTION
Avatar of naspinski
naspinski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kwh3856

ASKER

naspinski,
That is what I first thought when I read the error message but I checked by table and MPI has a field type of uniquie.  When I hover over .MPI1 it displays the message GuidMPI.MPI1.  Based on that I assume it is a GUID type.
When I created the initGuid, this is the code I used which initializes the variable as Guid.  I know I am missing something here but it is just not jumping out at me.  Here the code I use to create initGuid.
Could there be something else?
 

public static Guid initGuid
{
get
{
return Guid.NewGuid();
}
}
Avatar of kwh3856

ASKER

ooh...i will try that
Avatar of kwh3856

ASKER

ok.  When I did that, now I get
 '&&' cannot be applied to operands of type 'bool' and 'int?'
I thought when I got that error message I am supposed to used the single =
should i use .equals???
 
Avatar of kwh3856

ASKER

You were right.  I just did not see it.  Here was the correct code.

var patientlookup = from MPIREC in db.MPIs
                                                    where recordMPI.MPI1 == initGuid &&
                                                    recordMPI.NPI == Convert.ToInt32(ecDoctor.NPI)
                                                    select new { MPIREC };


Thanks once again.