.netcassandracassandra-2.1sorteddictionarydatastax-csharp-driver

Cassandra : map map<text, text> datatype of UDT to SortedDictionary in .Net


I am using cassandra DB for .Net. I have created a datatype dictionary_type_property map<text,text> inside user defined datatype, but facing problem while converting this datatype value to .Net dictionary type using udtMap.

.Map(s => s.MyDictionaryTypeProperty, "dictionary_type_property");

dictionary type property is:

public SortedDictionary<string, string> MyDictionaryTypeProperty { get; set; }

Here I am getting the below exception: "No converter is available from Type System.Collections.Generic.IDictionary2[System.String,System.String] is not convertible to type System.Collections.Generic.SortedDictionary2[System.String,System.String]"

Appreciate your help.


Solution

  • Well, as the error says, you are trying to convert an IDictionary variable to SortedDictionary. However, IDictionary does not inherit from SortedDictionary, it's the other way around. I suggest you to read about inheritance, if you never heard about.

    What I can suggest you, is to change

    public SortedDictionary<string, string> MyDictionaryTypeProperty { get; set; }
    

    to

    public IDictionary<string, string> MyDictionaryTypeProperty { get; set; }
    

    so this way,the method will try to "fit" an IDictionary value into a variable with the same type.

    .Map(s => s.MyDictionaryTypeProperty, "dictionary_type_property");
    

    if later you want to try to use this variable as a SortedDictionary, you might try using a cast (SortedDictionary) MyDictionaryTypeProperty

    or

        SortedDictionary<string, string> sortedDictionary = new SortedDictionary<string, string>(MyDictionaryTypeProperty);