Search code examples
sql-serverlinqlinqpad

Linq query using a field that's type uniqueidentifier


I'm trying to do something that seems like it should be super simple.

I have a table MyData that has a MyTableID column defined as uniqueidentifier:

CREATE TABLE [dbo].[MyTable]
(
    [MyTableID] [uniqueidentifier] NOT NULL,
    [SomeField] [int] NULL,

    CONSTRAINT [PK_MyTable] 
        PRIMARY KEY CLUSTERED ([MyTableID] ASC)
                WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                      IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                      ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

INSERT INTO MyTable (MyTableID, SomeField)
VALUES ('A267715B-0CB0-4203-AB2F-002321DC521A', 1)

INSERT INTO MyTable (MyTableID, SomeField)
VALUES ('A267715B-0CB0-4203-AB2F-002321DC521B', 1)

I'm trying to query it with LINQPad 7 like so:

MyTables.Where (m => m.MyTableID="A267715B-0CB0-4203-AB2F-002321DC521A")

but I get an error

Cannot implicitly convert type 'string to 'System.Guid'

I've tried rewriting it to

MyTables.Where (m => m.MyTableID = Guid.Parse("A267715B-0CB0-4203-AB2F-002321DC521A"))

But I get another error:

Cannot implicitly convert type 'System.Guid' to 'bool'

How can I query this table in Linq?


Solution

  • You were all the way there! You were just missing equal sign to denote equality.

    MyTables.Where (m => m.MyTableID == Guid.Parse("A267715B-0CB0-4203-AB2F-002321DC521A"))