locked
How to query on an Entity attribute key only when it's of type KeyValuePair ? RRS feed

  • Question

  • Hi,
    I have an entity contact with an attribute of type "KeyValuePair", the key name is "degree" and the value depends from one contact to another.

    I need to set the value of the degree of each contact, so I want to basically write something like this :

    contact.Attributes["degree"].Value=123;

    But this statement isn't accepted, it says that the attributes is expecting a KeyValuePair type not just a key, but I can't do that because that's what i want to set.

    Can you please help ?
    Thanks

    Tuesday, January 21, 2014 9:28 AM

All replies

  • Hi,
    there is no such an attribute type as "KeyValuePair" for crm entities, please check the attribute definition inside your entity.
    If for example is a whole number, just set in this way:

    contact["new_degree"] = 123;

    and pay attention also to the logicalname (if is a custom attribute it will contain the prefix)


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Proposed as answer by Guido PreiteMVP Tuesday, January 21, 2014 9:32 AM
    • Unproposed as answer by SB User Tuesday, January 21, 2014 11:07 AM
    Tuesday, January 21, 2014 9:32 AM
  • Hi,

    Try to add attribute in contact entity like :

    contact.Attributes["new_degree"]=123;



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog

    Tuesday, January 21, 2014 9:37 AM
  • Hi Guido, Thank you for your reply.

    I tried what you just wrote but it said : "Cannot apply indexing with [] to an expression of type Entity"


    EDIT:

    That's the rest of this part in my code:

     foreach (var contact in result.Entities)
                    {
                        var studentdegree = contact["degree"];

                          ............
                      }

    • Edited by SB User Tuesday, January 21, 2014 9:51 AM more clarification
    Tuesday, January 21, 2014 9:47 AM
  • Hi _Vikram,
    I've been trying to do this:
     foreach (var contact in result.Entities)
                    {
                        var studentdegree = contact.Attributes["degree"];
                         .........
                    }

    also I've tried : contact.Attributes["degree"].Value;

    but this is the error i receive :


    • Edited by SB User Tuesday, January 21, 2014 10:39 AM
    Tuesday, January 21, 2014 10:38 AM