17

When I run following in the LinqPad

var ProductIds = from p in Products 
where p.Id = "F1FE990C-4525-4BFE-9E2C-A7AFFF0DDA1F"
select p;

ProductIds.Dump();

it gives me

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

I just don't know how to apply proper cast it to GUid I guess

Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
Silverlight Student
  • 3,968
  • 10
  • 37
  • 53

4 Answers4

35

Try using the Guid.Parse(string guid) static method.

var ProductIds = from p in Products 
where p.Id == Guid.Parse("F1FE990C-4525-4BFE-9E2C-A7AFFF0DDA1F")
select p;

ProductIds.Dump();
Nathan Anderson
  • 6,768
  • 26
  • 29
1

You currently have an assignment, but you want to use a comparison - use == instead of = :

var ProductIds = from p in Products 
                 where p.Id == Guid.Parse("F1FE990C-4525-4BFE-9E2C-A7AFFF0DDA1F")
                 select p;
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
0

You can also set a variable using:

Guid guid = new Guid ("F1FE990C-4525-4BFE-9E2C-A7AFFF0DDA1F");

Then

var ProductIds = from p in Products
where p.Id == guid
select p;

ProductIds.Dump();
Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
0

You cannot cast it, you have to parse it like that:

where p.Id = Guid.Parse("F1FE990C-4525-4BFE-9E2C-A7AFFF0DDA1F");
Fischermaen
  • 12,238
  • 2
  • 39
  • 56