tags:

views:

78

answers:

2

So I will have a fairly large list of plot keywords in an xml for each movie, where they will be fed into each Movie instance that I create in memory.

But I don't know how I should implement this?

It's similar to IMDb's.

I want this to give me the ability to find all movies that have say 'Car Crash' or 'Deception'. or 'Scene After End Credits' plot keywords etc.

How should define this type? Should it be a list? How can I best implement this? Of top off my head I can use List and then see if the request keyword(s) are in each Movie's PlotKeywords list. But I feel like there is a better way to model this and get the result faster or cleaner, etc.

Should plot keywords be strings or a custom type? How should they be defined and stored?

Any ideas?

+2  A: 

I would treat the keywords similar to 'tags' is other applications. That's essentially what they come down to. So it would be a many-to-many relationship between a Tag and Movie

Agent_9191
Thanks, sounds good. What do you mean by many to many? Can you please give some examples?
Joan Venge
Joan, I believe that by many-to-many the commenter is referring to a many-to-many relationship in a relational database. Solving your problem with a relational database is trivial, but doing it with traditional in-memory data structures could be quite complex to manage.You really ought to consider using a DB.
Adam Crossland
THanks by DB, do you mean a DB structure like Oracle? The data will be in an xml. Would this affect it?
Joan Venge
You could always write some code that loads the data FROM the XML, then creates entries in a DB structure like Oracle. Databases are optimized from the ground-up to make this sort of thing very fast and easy.
GWLlosa
I see. I am not very concerned about performance. As I can't see this taking a lot of processing time. One can only have a couple thousand movies in their collection at most I think.
Joan Venge
+1  A: 

If you're going to have this all work on a database, then the plot-keywords would have one table, the movies would have another, and you'd have an intersection-table for the keywords-to-movies relationship. That way, when you search on a keyword, you merely return the list of all movies that contain the keyword's ID in the movies-to-keywords table.

If you want to implement this "in-memory", you can construct a similar creation, whereby as you "load" each movie (from XML or whatever) you populate a Dictionary<string,List<Movie>> with the key being the keyword and the Movie being added to the list associated with that string. That way, later on, loading the list of movies that contain a keyword is very fast and easy.

GWLlosa
Thanks I see what you mean. I will do it in memory but when you have this Dictionary, how would you implement the multiple keyword search? For example list all the movies that contains this AND this AND (this OR that)?
Joan Venge
You can use LINQ to very quickly and easily search on in-memory collections, dictionaries included. Something like myDictionary.Where(kvp=>mySearchKeyWords.Contains(kvp.Key)).Select(kvp=>kvp.Value), for example
GWLlosa
Thanks it makes sense. I think that should make it easier.
Joan Venge
Errr... be careful - I'm pretty sure the where statement is going to result in iterating over ALL the entries in the dictionary. Remember that the implementation of Where for types of IEnumerable<T> has no knowledge of the indexing capabilities of containers such as Dictionary<K,T>.
Phil
To follow on - When you use the Where clause against a database you are using a *different* extension method, one which is aware of and uses the indexing capabilities of your database. When using a database the extension methods used come from the Queryable class. When using LINQ queries against most in-memory objects, you are using extension methods from the Enumerable class. The enumerable extension methods use brute force iteration over IEnumerable<T>, there's no use of indexes involved.
Phil
If you are interested in using indexed collections with LINQ, you might want to take a look at "i40 - Indexed Linq". I haven't tried it myself but it is aimed at supporting use by LINQ of indexed in-memory collections. http://www.codeplex.com/i4o
Phil
That's a good idea.
GWLlosa