Wednesday, 21 October 2015

Add Key Value into Dictionary and get value based on key

Note: agilityItemDetailDto.OwnershipRoles // it's custom collection

//Add Key and value into dictionary
Dictionary<string, string> OwnershipRoleHolders = new Dictionary<string, string>();
            if (agilityItemDetailDto.OwnershipRoles != null)
            {
                OwnershipRoleHolders =
                agilityItemDetailDto.OwnershipRoles.Select(
                                r =>
                                    new
                                    {
                                        Key = r.OwnershipRoleSingleDisplayText,
                                        Value = String.Join(";", r.RoleHolders.Select(h => h.Title).ToArray())
                                    }).ToDictionary(d => d.Key, d => d.Value);

            }
           // string val2;
  //get Value based on key . For eg -OwnershipRoleHolders.ContainsKey("<Pass key name>")
            if (OwnershipRoleHolders.ContainsKey("Approver"))
            {
                string val1;
                OwnershipRoleHolders.TryGetValue("Approver", out val1);
                ApproverRoleTitle = val1;
            }

No comments:

Post a Comment