Search code examples
c#asp.netcastingektron

C# converting int to 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?


Solution

  • 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);