转换为Guid但接收无法转换为int错误

时间:2021-01-04 21:17:21

I am trying to use a linq statement to pull a userName out of my database for a string. However I keep getting an error.

我试图使用linq语句从我的数据库中提取一个字符串的userName。但是我不断收到错误。

This is the error I am getting:

这是我得到的错误:

Cannot implicitly convert 'string' to 'int'

无法隐式将'string'转换为'int'

which is quite odd because I am trying to convert it to a Guid, not an int.

这很奇怪,因为我试图将它转换为Guid,而不是int。

here is the code:

这是代码:

string ticketChange = "The following change(s) have been made by " + db.Users.Where(u => u.UserId == new Guid["LoggedUserID"]).First().FullName + Environment.NewLine;

I am receiving the error on the "loggedUserID" which currently is a string.

我收到的“loggedUserID”错误,当前是一个字符串。

Here is the code where I define LoggedUserID when the user logs in. (I know this is not the best way to perform this, which I will change in the future but I need to get this fixed soon). It pulls the userID which is a quid out of the database and saves it as a string.

这是我在用户登录时定义LoggedUserID的代码。(我知道这不是执行此操作的最佳方式,我将来会更改,但我需要尽快修复)。它从数据库中提取出一个quid的userID,并将其保存为字符串。

Session["LoggedUserID"] = v.UserId.ToString();

I have also tried:

我也尝试过:

Guid.NewGuid(["LoggedUserID"])

but there is no overload method for NewGuid that takes any arguments.

但是NewGuid没有带任何参数的重载方法。

u.UserID is a Guid. So how do I convert the string to a Guid in this case for it to work?

u.UserID是Guid。那么在这种情况下如何将字符串转换为Guid才能使其工作?

2 个解决方案

#1


0  

I'm not entirely sure what you're trying to accomplish here. What is "LoggedUserId" supposed to be?

我不完全确定你在这里想要完成什么。什么是“LoggedUserId”应该是什么?

The error is related to new Guid["LoggedUserId"]. A Guid is not a dictionary, so I'm not sure why you think you can index it or that it would have a key named "LoggedUserId" implicitly, even if you could. You could pass something to the constructor: new Guid(...), however, the passed value must be something that can be converted to a Guid, which "LoggedUserId" is not.

该错误与新Guid [“LoggedUserId”]有关。 Guid不是字典,所以我不确定你为什么认为你可以索引它或者它会隐含地命名为“LoggedUserId”的键,即使你可以。你可以将一些东西传递给构造函数:new Guid(...),但是,传递的值必须是可以转换为Guid的东西,而“LoggedUserId”则不是。

#2


0  

With Chris's help this is what ended up working:

在Chris的帮助下,这就是最终的工作:

Guid g = Guid.Parse(Session["LoggedUserID"] as string);

string ticketChange = "The following change(s) have been made by "  + db.Users.Where(u => u.UserId == g).First().FullName + Environment.NewLine;

#1


0  

I'm not entirely sure what you're trying to accomplish here. What is "LoggedUserId" supposed to be?

我不完全确定你在这里想要完成什么。什么是“LoggedUserId”应该是什么?

The error is related to new Guid["LoggedUserId"]. A Guid is not a dictionary, so I'm not sure why you think you can index it or that it would have a key named "LoggedUserId" implicitly, even if you could. You could pass something to the constructor: new Guid(...), however, the passed value must be something that can be converted to a Guid, which "LoggedUserId" is not.

该错误与新Guid [“LoggedUserId”]有关。 Guid不是字典,所以我不确定你为什么认为你可以索引它或者它会隐含地命名为“LoggedUserId”的键,即使你可以。你可以将一些东西传递给构造函数:new Guid(...),但是,传递的值必须是可以转换为Guid的东西,而“LoggedUserId”则不是。

#2


0  

With Chris's help this is what ended up working:

在Chris的帮助下,这就是最终的工作:

Guid g = Guid.Parse(Session["LoggedUserID"] as string);

string ticketChange = "The following change(s) have been made by "  + db.Users.Where(u => u.UserId == g).First().FullName + Environment.NewLine;