C# 将 int 转换为 Int64

发布于 2024-09-29 15:11:39 字数 709 浏览 3 评论 0原文

我们正在升级我们蹩脚的 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 技术交流群。

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

发布评论

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

评论(5

娇纵 2024-10-06 15:11:39

由于 C# 是强类型的,所以不能这样转换。在传入之前,您需要将 Int64 转换为 Int32:

aliases.TryGetValue((int)recipe.Id, out alias)

或者,您可以更改字典的定义:

IDictionary<Int64, string> aliases 
    = new UrlAliasApi().GetUrlAliasesByType(Company.DataLayer.Enumeration.UrlAliasType.Recipe)
                       .ToDictionary(kvp => (Int64)key.Key, kvp => kvp.Value);

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:

aliases.TryGetValue((int)recipe.Id, out alias)

Alternatively, you can change the definition of your dictionary:

IDictionary<Int64, string> aliases 
    = new UrlAliasApi().GetUrlAliasesByType(Company.DataLayer.Enumeration.UrlAliasType.Recipe)
                       .ToDictionary(kvp => (Int64)key.Key, kvp => kvp.Value);
ぶ宁プ宁ぶ 2024-10-06 15:11:39

您需要将 Int64 转换为 int,如下所示:

aliases.TryGetValue((int)recipe.Id, out alias)

You need to cast the Int64 to an int, like this:

aliases.TryGetValue((int)recipe.Id, out alias)
谷夏 2024-10-06 15:11:39

请注意,虽然您可以将 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.

等数载,海棠开 2024-10-06 15:11:39

这里的问题是您试图在采用 int/Int32 类型的地方使用 Int64 值。这里没有隐式转换,因此会出现编译器错误。

解决此问题的最佳方法是将 aliases 字典也转换为使用 Int64 类型。将 int 转换为 Int64 始终是安全的,因此此转换中不会丢失任何信息。

理想情况下,您应该将 GetUrlAliasesByType 转换为返回 IDictionary。系统的其余部分现在使用 Int64,因此这种转换是有意义的。否则,您可以执行以下操作。

string alias; 
try { 
  if (aliases.TryGetValue(checked((int)recipe.Id), out alias)) 
  { 
    recipe.QuickLink = alias; 
  } 
} catch (OverflowException) { 
  // id not valid
}

此处的检查操作是必要的,因为它可以防止静默溢出与 TryGetValue 产生错误匹配

The problem here is that you are trying to use an Int64 value in a place which takes an int/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 an Int64 type as well. It's always safe to convert an int to Int64 so there is no information loss in this conversion.

Ideally you'd convert GetUrlAliasesByType to return an IDictionary<Int64,string>. The rest of the system is now using Int64 so this conversion makes sense. Otherwise you can do the following

string alias; 
try { 
  if (aliases.TryGetValue(checked((int)recipe.Id), out alias)) 
  { 
    recipe.QuickLink = alias; 
  } 
} catch (OverflowException) { 
  // id not valid
}

The checked operation here is necessary because it prevents a silent overflow from producing a false match with TryGetValue

反话 2024-10-06 15:11:39

为什么不能只使用 Dictionary 而不是 Dictionary

Why can't you just use Dictionary<Int64, string> instead of Dictionary<int, string>?

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