Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast string as Guid using LinqPad

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

like image 651
Silverlight Student Avatar asked Nov 09 '11 15:11

Silverlight Student


People also ask

How to convert string value to Guid in c#?

The Parse method trims any leading or trailing white space from input and converts the string representation of a GUID to a Guid value. This method can convert strings in any of the five formats produced by the ToString(String) and ToString(String, IFormatProvider) methods, as shown in the following table.

What is the use of LINQPad?

LINQPad lets you query Entity Framework models that you define in Visual Studio. This provides instant feedback, as well as enabling you to see the SQL that your queries generate (just click the SQL tab). You can query Entity Framework models in both LINQPad 7 (for . NET Core / .


2 Answers

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();
like image 97
Nathan Anderson Avatar answered Oct 20 '22 10:10

Nathan Anderson


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;
like image 38
BrokenGlass Avatar answered Oct 20 '22 10:10

BrokenGlass