locked
Dictionary.containskey() concatenate string issue RRS feed

  • Question

  • User-640323567 posted

    I have a dictionary which checks for a string value existence.

    The string value is a concatenation of two string variables.
    When dictionary.containskey(concatStringValue) gets executed, it always returns false even though the value exists in dictionary.

    I suspect it's the concatenation that is causing this issue as I have other dictionaries which looks for a string value(NOT concatenated) works just fine.

    I tried these both below ways to concatenate the string values but still the same:

     //string concatClientFullName = string.Format("{0}, {1}", ClientLastNameValue, ClientFirstNameValue);
       string concatClientFullName = ClientLastNameValue + ", " + ClientFirstNameValue;
    
    Dictionary<string, Guid> dictLookupClientName = new Dictionary<string, Guid>(StringComparer.OrdinalIgnoreCase);
    
     foreach (Entity clientname in ClientNameCollection.Entities)
                                            {
                                                string clientnameName = clientname.GetAttributeValue<string>("new_name");
    
                                                if (!dictLookupClientName.ContainsKey(clientnameName))
                                                {
                                                    dictLookupClientName.Add(clientnameName, clientname.Id);
                                                }
                                            }
    
                                            if (dictLookupClientName.ContainsKey(concatClientFullName))//Fails here and goes to else part
                                            {
    
                                            }

    Can someone please point me in right direction to fix this issue. Thanks for any help.

    Tuesday, September 20, 2016 8:26 PM

All replies

  • User753101303 posted

    Hi,

    Double check concatClientFullName and the key you expect to find inside the dictionary (using for example the Keys property). This is is in the same last name, first name orde, it uses , and a single space in between etc...

    Not directly related but could it be a logic error. For now ity seems you are adding a value inside a loop and you check the whole dictionary after having added each new value. Either it could be done more efficiently or perhaps it doesn't do what you really intended ?

    Tuesday, September 20, 2016 8:40 PM
  • User347430248 posted

    Hi Aspbun,

    did you try to check with only using ClientLastNameValue or ClientFirstNameValue without concatenating it.

    is it worked?

    if so did you try to append the value instead of concatenating it.

    maybe work here

    Regards

    Deepak

    Wednesday, September 21, 2016 2:59 AM
  • User-640323567 posted

    Hi Deepak,

    I tried with only firstname and it worked perfectly. I also used stringbuilder.append, string.join,string.concat but that none of them work either. Dictionary.containskey() won't recognize my concatenated string at all. Any other suggestions please?

    Thank you

    Wednesday, September 21, 2016 1:06 PM
  • User753101303 posted

    Have you tried to check which value you get from this "new_name" attribute?

    For now it seems the issue is that you are just not consistent ie it seems the FirstName is used as a key to store data while first name/last name is used as key to retrieve data which of course won't work.

    The problem is not with the concatenation but likely just with making sure you are using always the same key to store and retrieve data.

    You have to be consistent. Note also that it might be not impossible to have 2 persons with the same first name/last name which might cause issues. So the big picture would help but I would match customers based on some customer id (clientname.Id) for example rather than just in a first name/last name.

    Wednesday, September 21, 2016 1:56 PM
  • User303363814 posted

    Set a breakpoint on the line which fails.  What is the value of concatClientFullName?  How long is the string (don't count by eye, use .Length)?

    What are the values of the keys in the dictionary?  What is the length of each key (again, measure - don't count by eye)?

    <<< BTW - What is the reason for the 'strange' way to get the value of the new_name attribute?  Is there a reason you don't use .ToDictionary() to populate the dictionary? >>>

    Thursday, September 22, 2016 8:23 AM