Cast string as Guid using LinqPad

14,671

Solution 1

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();

Solution 2

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;
Share:
14,671
Silverlight Student
Author by

Silverlight Student

Updated on June 22, 2022

Comments

  • Silverlight Student
    Silverlight Student almost 2 years

    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

  • Silverlight Student
    Silverlight Student over 12 years
    Already tried it but that give me "Cannot implicitly convert type 'System.Guid' to 'bool'" error
  • Silverlight Student
    Silverlight Student over 12 years
    Please see my response to @Nathan comment
  • Nathan Anderson
    Nathan Anderson over 12 years
    Make sure you have == between the comparison of p.Id and your Guid.Parse() statements, not a single =.
  • Silverlight Student
    Silverlight Student over 12 years
    :) i must be drunk. Thanks Dude
  • Fischermaen
    Fischermaen over 12 years
    So try Guid.Parse("F1FE990C-4525-4BFE-9E2C-A7AFFF0DDA1F").CompareTo‌​(p.Id) = 0
  • Silverlight Student
    Silverlight Student over 12 years
    Sorry for the trouble. I guess I have drinking problem :)
  • Ravi Ram
    Ravi Ram over 9 years
    @NathanAnderson Using LinqPad I am getting an error: 'System.Guid' does not contain a definition for 'Parse'
  • Nathan Anderson
    Nathan Anderson over 9 years
    Try using the constructor that takes a string as an argument. msdn.microsoft.com/en-us/library/96ff78dc%28v=vs.110%29.aspx