//TODO: Refactor
 public void addCategoriesToDocument(List<string> categoryList, int documentId)
 {
     List<Category> allCategories = (
                                     from c in this.Categories
                                     select c
                                     ).ToList();
     List<string> allCategoryNames = new List<string>();
     foreach(Category cat in allCategories)
     {
         allCategoryNames.Add(cat.Name);
     }
     foreach (string categoryToBeAdded in categoryList)
     {
         if (allCategoryNames.Contains(categoryToBeAdded))
         {
             //No need to add the category. Just add the association
             Category category = allCategories.Find(delegate(Category c) { return c.Name == categoryToBeAdded; });
             CategoryDocument catDoc = new CategoryDocument { CategoryId = category.Id, DocumentId = documentId };
             CategoryDocuments.InsertOnSubmit(catDoc);
             this.SubmitChanges();
         }
         else
         {
             //The category does not already exist. Add a new category object too.
             Category category = new Category { Name = categoryToBeAdded };
             Categories.InsertOnSubmit(category);
             this.SubmitChanges();
             Category insertedCategory = (from c in this.Categories
                                          where c.Name == categoryToBeAdded
                                          select c).First();
             CategoryDocument catDoc = new CategoryDocument { DocumentId = documentId, CategoryId = insertedCategory.Id };
             CategoryDocuments.InsertOnSubmit(catDoc);
             this.SubmitChanges();
         }
     }
 }
 partial void DeleteCategory(Category instance);
 partial void UpdateCategory(Category instance);
 partial void InsertCategory(Category instance);