+ Reply to Thread
Results 1 to 2 of 2

Thread: How to do like on dictionary?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    10

    How to do like on dictionary?

    I'm currently using this method :
    Code:
    mydict.ContainsKey(keyName);
    But some keyNames have an additional word appended like :
    Code:
    "key1" == "key1"  //match
    "key1" == "key1 someword" //partial match
    I'd like to do a "like" or .StartsWith() , i need to match in both cases.

  2. #2
    For matching and retrieving keys "like" you could use these extensions.
    Code:
    public static class Extensions
    {
        public static bool HasKeyLike<T>(this Dictionary<string, T> collection, string value)
        {
            var keysLikeCount = collection.Keys.Count(x => x.ToLower().Contains(value.ToLower()));
            return keysLikeCount > 0;
        }
    
        public static List<string> GetKeysLike<T>(this Dictionary<string, T> collection, string value)
        {
            return collection.Keys.Select(x => x.ToLower().Contains(value.ToLower())).ToList();
        }
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts