CRM 2011 – How to use guids in LINQ queries

This took me a bit of time to work out but in the end it’s very simple.  You can’t just reference a guid as a string, you have to create a guid type or cast it to be a guid.  Amusingly I had to select the guid to make find out what the guid was which totally defeats the object but I wanted to know what it was.

In the end it turned out my initially code didn’t work because I had missed a digit off, .NET to be fair did tell me my guid need to be 32 digits.

Create a guid

Guid myGuid = new Guid(“92eb0d0e-b22e-e011-9645-00155d106b02”);                             IEnumerable<Account> accounts = from a in orgContext.CreateQuery<Account>()

where (a.Id == myGuid)

{

Name = a.Name,

Address1_County = a.Address1_County,

Id = a.Id

};

Casting the String to a guid

IEnumerable<Account> accounts = from a in orgContext.CreateQuery<Account>()

where (a.Id == new Guid(“92eb0d0e-b22e-e011-9645-00155d106b02”))

{

Name = a.Name,

Address1_County = a.Address1_County,

Id = a.Id

};

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.