Querying a GUID field using Linq
Posted: Sat 09 Oct 2010 10:45
Posted: Sat 09 Oct 2010 10:42 Post subject: Guids and Linq
--------------------------------------------------------------------------------
I have a database I designed that contains a primary key I designed as System.Guid. The Server Data Type is defined as CHAR(36). I am able to use linq to add records.
The following query works if I'm trying to retrieve all donors:
Donors = From Info In DataContext.Donors Select Info
For Each Donor In Donors
'do stuff
Next
When I try to retrieve a single record:
Donor = (From Info In DataContext.Donor Where Info.ID = SomeGuid Select Info).FirstOrDefault
I get a runtime error that says "Cannot use "CHAR(255)" type value in "Guid" type argument". I had also tried defining the ID column as BINARY(16) and would get a similar error.
How can I use Linq to query a single record and use the Guid field?
--------------------------------------------------------------------------------
I have a database I designed that contains a primary key I designed as System.Guid. The Server Data Type is defined as CHAR(36). I am able to use linq to add records.
The following query works if I'm trying to retrieve all donors:
Donors = From Info In DataContext.Donors Select Info
For Each Donor In Donors
'do stuff
Next
When I try to retrieve a single record:
Donor = (From Info In DataContext.Donor Where Info.ID = SomeGuid Select Info).FirstOrDefault
I get a runtime error that says "Cannot use "CHAR(255)" type value in "Guid" type argument". I had also tried defining the ID column as BINARY(16) and would get a similar error.
How can I use Linq to query a single record and use the Guid field?