C# 将 int 转换为 Int64

标签 c# asp.net casting ektron

我们正在升级糟糕的 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;
   }
}

错误是

Error 323 The best overloaded method match for 'System.Collections.Generic.IDictionary.TryGetValue(int, out string)' has some invalid arguments

它指的是 recipe.Id,它是一个 Int64 值。

有什么解决办法吗?

最佳答案

因为 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);

关于C# 将 int 转换为 Int64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4055389/

相关文章:

c++ - 将实例转换为无效?

sql - 在sqlite中将int转换为real

c# - 为什么 C#.net 中的表达式主体不能使用 int、double 或 bool 类型的属性?

c# - 在哪里可以下载 Resharper 的命名标准/编码标准?

asp.net - 从中继器项目模板内部,如何设置输入类型(图像)控件标题属性?

asp.net - web.config 是否比数据库更安全?

c# - XUnit 测试 DbContext 没有处理

c# - driver.Close 后如何从 ChromeDriver 重新打开浏览器?

支持拖放的 ASP.NET Treeview 控件?

c# - 指定的转换无效 C# Entity Framework