C# 将 int 转换为 Int64
我们正在升级我们蹩脚的 cms 系统,新的程序集已从 int 更改为 int64。我现在尝试构建时遇到了问题。我尝试过铸造,但似乎没有帮助。这是导致问题的代码摘录之一。
IDictionary<int, string> aliases
= new UrlAliasApi().GetUrlAliasesByType(
Company.DataLayer.Enumeration.UrlAliasType.Recipe);
foreach (ContentBase recipe in mergedResultset)
{
// if alias exists, overwrite quicklink!
string alias;
if (aliases.TryGetValue(recipe.Id, out alias))
{
recipe.QuickLink = alias;
}
}
错误是
错误 323 与“System.Collections.Generic.IDictionary.TryGetValue(int, out string)”匹配的最佳重载方法有一些无效参数
它引用的是 recipe.Id
,它是一个 Int64< /代码> 值。
有什么想法来处理这个问题吗?
we are in the process of upgrading our crappy cms system and the new assemblies have changed from int to int64. I'm running into a problem when trying to build now. I've tried casting but it doesnt seem to help. here is one excerpt of code that is causing a problem.
IDictionary<int, string> aliases
= new UrlAliasApi().GetUrlAliasesByType(
Company.DataLayer.Enumeration.UrlAliasType.Recipe);
foreach (ContentBase recipe in mergedResultset)
{
// if alias exists, overwrite quicklink!
string alias;
if (aliases.TryGetValue(recipe.Id, out alias))
{
recipe.QuickLink = alias;
}
}
The error is
Error 323 The best overloaded method match for 'System.Collections.Generic.IDictionary.TryGetValue(int, out string)' has some invalid arguments
Its referring to recipe.Id
which is an Int64
value.
Any ideas of to deal with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于 C# 是强类型的,所以不能这样转换。在传入之前,您需要将 Int64 转换为 Int32:
或者,您可以更改字典的定义:
Since C# is strongly typed, you can't convert like this. You need to cast the Int64 to an Int32 before you pass it in:
Alternatively, you can change the definition of your dictionary:
您需要将
Int64
转换为int
,如下所示:You need to cast the
Int64
to anint
, like this:请注意,虽然您可以将 Int64 转换为 Int32(或 int),但如果您要转换的值大于 Int32 中可以表示的值,则可能会丢失一些数据。就你而言,这可能并不重要,但我想我应该提一下。
Note that while you can cast Int64 to Int32 (or int), you could lose some data if the values that you are casting are larger than can be represented in an Int32. In your case it might not matter, but I thought I should mention it.
这里的问题是您试图在采用
int/Int32
类型的地方使用Int64
值。这里没有隐式转换,因此会出现编译器错误。解决此问题的最佳方法是将
aliases
字典也转换为使用Int64
类型。将int
转换为Int64
始终是安全的,因此此转换中不会丢失任何信息。理想情况下,您应该将
GetUrlAliasesByType
转换为返回IDictionary
。系统的其余部分现在使用 Int64,因此这种转换是有意义的。否则,您可以执行以下操作。此处的检查操作是必要的,因为它可以防止静默溢出与
TryGetValue
产生错误匹配The problem here is that you are trying to use an
Int64
value in a place which takes anint/Int32
type. There is no implicit conversion here and hence the compiler errors.The best way to fix this is to convert the
aliases
dictionary to use anInt64
type as well. It's always safe to convert anint
toInt64
so there is no information loss in this conversion.Ideally you'd convert
GetUrlAliasesByType
to return anIDictionary<Int64,string>
. The rest of the system is now usingInt64
so this conversion makes sense. Otherwise you can do the followingThe checked operation here is necessary because it prevents a silent overflow from producing a false match with
TryGetValue
为什么不能只使用
Dictionary
而不是Dictionary
?Why can't you just use
Dictionary<Int64, string>
instead ofDictionary<int, string>
?