0

I have a movie cache dictionary where key is string and value is movie.

Dictionary<string, Movie> movieCache = new Dictionary<string, Movie>();

movieCache.Add(string.Format("{0}:{1}:{2}", newMovie.Year, newMovie.Title, newMovie.Genre), newMovie);

I have another list say joe's favorite movie list. for example

List<IMDBMovie> joesFavMovie // some list;

I need to display two lists. One: the movies from the cache that is in joesFavMovie and matches the year, title and Genre. Two: the movies from the cache that is in joesFavMovie and matches the year, title.

I was able to display the first list form the cache. However i was creating another dictionary cache from the existing dictionary to display the second list. Is there a way to use the same cache for both the lists. I am not sure if wild card is the best solution.

Thanks in advance. The title might not be appropriate. please suggest if that needs to be changed.

IamaC
  • 357
  • 1
  • 10
  • 23
  • 1
    You have a typo in your code (Chace instead of Cache) – Kevin Dec 23 '14 at 13:44
  • 1
    Could you please clarify where do you use wildcard? The title is a bit misleading. Thanks and regards, – Alexander Bell Dec 23 '14 at 13:49
  • 1
    The second line of code still says "movieChace". – Tony Vitabile Dec 23 '14 at 13:55
  • @AlexBell, Thanks. I would like to use the wild card for key. Updated the title. – IamaC Dec 23 '14 at 13:56
  • 1
    See this [answer](http://stackoverflow.com/a/9672624/945456). There is no wildcard search of dictionary keys without losing the O(1) lookup it provides. If performance really is critical (have you measured this and found it to be a bottleneck in your program that's causing problems?) then you may have to chose a different data-structure. – Jeff B Dec 23 '14 at 14:10
  • @JeffBridgman I just created this example so that I can explain. It is not the actual scenario. However the problem is the same. Could you please suggest the architecture I should use? – IamaC Dec 23 '14 at 14:29

2 Answers2

1

You can use LINQ for this.

List<Movie> movies = 
     movieCache.Where(m => m.Value.Year == 1975 && m.Value.Title == "Jaws")
               .ToList();
Anthony Faull
  • 17,549
  • 5
  • 55
  • 73
  • The lambda expression could easily use a Regex expression to do the matching, providing you a mechanism for using wildcards. – Tony Vitabile Dec 23 '14 at 13:57
  • @Anthony Faull. thanks for your answer. I am looking for more efficient solution where i get to use the cache as dictionary. something O(n) – IamaC Dec 23 '14 at 13:58
  • 1
    @lamaC I'm pretty sure this answer is O(n); it only has to iterate once over the *n* items in the dictionary, checking which ones match along the way. Dictionary look-up using the key (e.g. `movieCache["1975:Jaws:Thriller"]`) is O(1). – Jeff B Dec 23 '14 at 14:07
  • @JeffBridgman, Please let me know if I am wrong here. If i run a foreach on joesFavMovie and then do a where on the cache that is O(n*n) right? if you are talking about the first list in my question then its O(n). I am trying to achieve same for the second. – IamaC Dec 23 '14 at 14:19
  • If they're nested then yes. You didn't have any code for that part in your question, so that escaped my notice. If the loops aren't nested, then it's just two O(n) (`2n` vs `n*n`). – Jeff B Dec 23 '14 at 14:35
0
var list2 = movieCache.Where(
    m => joesFavMovie.Any(fm => fm.Year == m.Value.Year && 
                                fm.Title == m.Value.Title)).ToList();
Jeff B
  • 8,572
  • 17
  • 61
  • 140
Kelvin Lai
  • 2,209
  • 7
  • 24
  • 26