使用 LinqPad 将字符串转换为 Guid

发布于 2024-12-15 03:35:01 字数 287 浏览 4 评论 0原文

当我在 LinqPad 中运行以下命令时,

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

ProductIds.Dump();

它给了我

无法将类型“string”隐式转换为“System.Guid”

我只是不知道如何将其正确转换为 GUId 我猜

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离旧人 2024-12-22 03:35:01

尝试使用 Guid.Parse(string guid) 静态方法。

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

ProductIds.Dump();

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();
笔落惊风雨 2024-12-22 03:35:01

您当前有一个作业,但您想使用比较 - 使用 == 而不是 =

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

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;
晨与橙与城 2024-12-22 03:35:01

你不能投射它,你必须像这样解析它:

where p.Id = Guid.Parse("F1FE990C-4525-4BFE-9E2C-A7AFFF0DDA1F");

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

where p.Id = Guid.Parse("F1FE990C-4525-4BFE-9E2C-A7AFFF0DDA1F");
看春风乍起 2024-12-22 03:35:01

方法设置变量:

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

您还可以使用以下

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

ProductIds.Dump();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文