public const double PriceOfSingleSolution = 0.60; //Single

        #endregion Fields

        #region Methods

        public bool AddSolution(User theUser, SelectModel model, AnswerAppDataContext db)
        {
            String[] UserAnswers;
            if(theUser.Answers != null)
            {
                UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                foreach (String Answer in UserAnswers)
                {
                    String thisAnswer = Answer.Replace(".pdf", "");
                    if (model.Contains(thisAnswer))
                    {
                        theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                    }
                }
            }
            theUser.Answers += model.ToString() + ".pdf;";

            bool done = false;
            if (!model.Question.Equals("All") && !done)
            {
                //Find all questions from the selected section
                IQueryable<AnswerApp.Models.Question> retrieved = from theAnswers in db.Questions
                                                                  where theAnswers.Page_Number.Equals(model.Page)
                                                                  select theAnswers;
                Question[] results = retrieved.ToArray<Question>();
                bool UserHasAll = true;
                foreach (Question theQuestion in results)
                {
                    if (!UserHasAccess(theUser, theQuestion.Textbook_Title + "_" + theQuestion.Unit_Title + "_" + theQuestion.Chapter_Title + "_" + theQuestion.Section_Title + "_" + theQuestion.Page_Number + "_" + theQuestion.Question_Number + ".pdf", db))
                    {
                        UserHasAll = false;
                        break;
                    }
                }
                if (UserHasAll)
                {
                    model.Question = "All";
                    UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                    foreach (String Answer in UserAnswers)
                    {
                        String thisAnswer = Answer.Replace(".pdf", "");
                        if (model.Contains(thisAnswer))
                        {
                            theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                        }
                    }
                    theUser.Answers += model.ToString() + ".pdf;";
                }
                else
                {
                    done = true;
                }
            }
            if (!model.Page.Equals("All") && !done)
            {
                //Find all questions from the selected section
                IQueryable<AnswerApp.Models.Page> retrieved = from thePages in db.Pages
                                                              where thePages.Section_Title.Equals(model.Section)
                                                              select thePages;
                AnswerApp.Models.Page[] results = retrieved.ToArray<AnswerApp.Models.Page>();
                bool UserHasAll = true;
                foreach (AnswerApp.Models.Page thePage in results)
                {
                    if (!UserHasAccess(theUser, thePage.Textbook_Title + "_" + thePage.Unit_Title + "_" + thePage.Chapter_Title + "_" + thePage.Section_Title + "_" + thePage.Page_Number + "_All", db))
                    {
                        UserHasAll = false;
                        break;
                    }
                }
                if (UserHasAll)
                {
                    model.Page = "All";
                    UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                    foreach (String Answer in UserAnswers)
                    {
                        String thisAnswer = Answer.Replace(".pdf", "");
                        if (model.Contains(thisAnswer))
                        {
                            theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                        }
                    }
                    theUser.Answers += model.ToString() + ".pdf;";
                }
                else
                {
                    done = true;
                }
            }
            if (!model.Section.Equals("All") && !done)
            {
                //Find all Secrtions from the selected Chapter
                IQueryable<AnswerApp.Models.Section> retrieved = from theSections in db.Sections
                                                                 where theSections.Chapter_Title.Equals(model.Chapter)
                                                                 select theSections;
                AnswerApp.Models.Section[] results = retrieved.ToArray<AnswerApp.Models.Section>();
                bool UserHasAll = true;
                foreach (AnswerApp.Models.Section theSection in results)
                {
                    if (!UserHasAccess(theUser, theSection.Textbook_Title + "_" + theSection.Unit_Title + "_" + theSection.Chapter_Title + "_" + theSection.Section_Title + "_All_All", db))
                    {
                        UserHasAll = false;
                        break;
                    }
                }
                if (UserHasAll)
                {
                    model.Section = "All";
                    UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                    foreach (String Answer in UserAnswers)
                    {
                        String thisAnswer = Answer.Replace(".pdf", "");
                        if (model.Contains(thisAnswer))
                        {
                            theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                        }
                    }
                    theUser.Answers += model.ToString() + ".pdf;";
                }
                else
                {
                    done = true;
                }
            }
            if (!model.Chapter.Equals("All") && !done)
            {
                //Find all Chapters from the selected Unit
                IQueryable<AnswerApp.Models.Chapter> retrieved = from theChapters in db.Chapters
                                                                 where theChapters.Unit_Title.Equals(model.Unit)
                                                                 select theChapters;
                AnswerApp.Models.Chapter[] results = retrieved.ToArray<AnswerApp.Models.Chapter>();
                bool UserHasAll = true;
                foreach (AnswerApp.Models.Chapter theChapter in results)
                {
                    if (!UserHasAccess(theUser, theChapter.Textbook_Title + "_" + theChapter.Unit_Title + "_" + theChapter.Chapter_Title + "_All_All_All", db))
                    {
                        UserHasAll = false;
                        break;
                    }
                }
                if (UserHasAll)
                {
                    model.Chapter = "All";
                    UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                    foreach (String Answer in UserAnswers)
                    {
                        String thisAnswer = Answer.Replace(".pdf", "");
                        if (model.Contains(thisAnswer))
                        {
                            theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                        }
                    }
                    theUser.Answers += model.ToString() + ".pdf;";
                }
                else
                {
                    done = true;
                }
            }
            if (!model.Unit.Equals("All") && !done)
            {
                //Find all Units from the selected Textbook
                IQueryable<AnswerApp.Models.Unit> retrieved = from theUnits in db.Units
                                                              where theUnits.Textbook_Title.Equals(model.Textbook)
                                                              select theUnits;
                AnswerApp.Models.Unit[] results = retrieved.ToArray<AnswerApp.Models.Unit>();
                bool UserHasAll = true;
                foreach (AnswerApp.Models.Unit theUnit in results)
                {
                    if (!UserHasAccess(theUser, theUnit.Textbook_Title + "_" + theUnit.Unit_Title + "_All_All_All_All", db))
                    {
                        UserHasAll = false;
                        break;
                    }
                }
                if (UserHasAll)
                {
                    model.Unit = "All";
                    UserAnswers = theUser.Answers.Split(new char[1] { ';' });
                    foreach (String Answer in UserAnswers)
                    {
                        String thisAnswer = Answer.Replace(".pdf", "");
                        if (model.Contains(thisAnswer))
                        {
                            theUser.Answers = theUser.Answers.Replace(thisAnswer + ".pdf;", "");
                        }
                    }
                    theUser.Answers += model.ToString() + ".pdf;";
                }
                else
                {
                    done = true;
                }
            }
            db.SubmitChanges();
            return true;
        }
 public bool AddSolutionByCredit(AnswerApp.Models.User theUser, AnswerAppDataContext db, SelectModel model)
 {
     if (model.Question.Equals("All"))
     {
         if (model.Page.Equals("All"))
         {
             if (model.Section.Equals("All"))
             {
                 if (model.Chapter.Equals("All"))
                 {
                     if (model.Unit.Equals("All"))
                     {
                         if (model.Textbook.Equals("All"))
                         {
                             IQueryable<Question> retrieved = from theAnswers in db.Questions
                                                              select theAnswers;
                             Question[] results = retrieved.ToArray<Question>();
                             int ResultsUserAlreadyHas = 0;
                             foreach (Question theQuestion in results)
                             {
                                 SelectModel theModel = new SelectModel(theQuestion);
                                 if (UserHasAccess(theUser.UserName, theModel.ToString()))
                                 {
                                     ResultsUserAlreadyHas += NumberOfQuestions(theModel, db);
                                 }
                             }
                             int SolutionsToAdd = results.Length - ResultsUserAlreadyHas;
                             if (SolutionsToAdd <= theUser.Credit)
                             {
                                 theUser.Credit -= SolutionsToAdd;
                                 AddSolution(theUser, model, db);
                                 db.SubmitChanges();
                             }
                             else
                             {
                                 return false;
                             }
                         }
                         else
                         {
                             IQueryable<Question> retrieved = from theAnswers in db.Questions
                                                              where theAnswers.Textbook_Title.Equals(model.Textbook)
                                                              select theAnswers;
                             Question[] results = retrieved.ToArray<Question>();
                             int ResultsUserAlreadyHas = 0;
                             foreach (Question theQuestion in results)
                             {
                                 SelectModel theModel = new SelectModel(theQuestion);
                                 if (UserHasAccess(theUser.UserName, theModel.ToString()))
                                 {
                                     ResultsUserAlreadyHas += NumberOfQuestions(theModel, db);
                                 }
                             }
                             int SolutionsToAdd = results.Length - ResultsUserAlreadyHas;
                             if (SolutionsToAdd <= theUser.Credit)
                             {
                                 theUser.Credit -= SolutionsToAdd;
                                 AddSolution(theUser, model, db);
                                 db.SubmitChanges();
                             }
                             else
                             {
                                 return false;
                             }
                         }
                     }
                     else
                     {
                         IQueryable<Question> retrieved = from theAnswers in db.Questions
                                                          where theAnswers.Textbook_Title.Equals(model.Textbook)
                                                          && theAnswers.Unit_Title.Equals(model.Unit)
                                                          select theAnswers;
                         Question[] results = retrieved.ToArray<Question>();
                         int ResultsUserAlreadyHas = 0;
                         foreach (Question theQuestion in results)
                         {
                             SelectModel theModel = new SelectModel(theQuestion);
                             if (UserHasAccess(theUser.UserName, theModel.ToString()))
                             {
                                 ResultsUserAlreadyHas += NumberOfQuestions(theModel, db);
                             }
                         }
                         int SolutionsToAdd = results.Length - ResultsUserAlreadyHas;
                         if (SolutionsToAdd <= theUser.Credit)
                         {
                             theUser.Credit -= SolutionsToAdd;
                             AddSolution(theUser, model, db);
                             db.SubmitChanges();
                         }
                         else
                         {
                             return false;
                         }
                     }
                 }
                 else
                 {
                     IQueryable<Question> retrieved = from theAnswers in db.Questions
                                                      where theAnswers.Textbook_Title.Equals(model.Textbook)
                                                      && theAnswers.Unit_Title.Equals(model.Unit)
                                                      && theAnswers.Chapter_Title.Equals(model.Chapter)
                                                      select theAnswers;
                     Question[] results = retrieved.ToArray<Question>();
                     int ResultsUserAlreadyHas = 0;
                     foreach (Question theQuestion in results)
                     {
                         SelectModel theModel = new SelectModel(theQuestion);
                         if (UserHasAccess(theUser.UserName, theModel.ToString()))
                         {
                             ResultsUserAlreadyHas += NumberOfQuestions(theModel, db);
                         }
                     }
                     int SolutionsToAdd = results.Length - ResultsUserAlreadyHas;
                     if (SolutionsToAdd <= theUser.Credit)
                     {
                         theUser.Credit -= SolutionsToAdd;
                         AddSolution(theUser, model, db);
                         db.SubmitChanges();
                     }
                     else
                     {
                         return false;
                     }
                 }
             }
             else
             {
                 IQueryable<Question> retrieved = from theAnswers in db.Questions
                                                  where theAnswers.Textbook_Title.Equals(model.Textbook)
                                                  && theAnswers.Unit_Title.Equals(model.Unit)
                                                  && theAnswers.Chapter_Title.Equals(model.Chapter)
                                                  && theAnswers.Section_Title.Equals(model.Section)
                                                  select theAnswers;
                 Question[] results = retrieved.ToArray<Question>();
                 int ResultsUserAlreadyHas = 0;
                 foreach (Question theQuestion in results)
                 {
                     SelectModel theModel = new SelectModel(theQuestion);
                     if (UserHasAccess(theUser.UserName, theModel.ToString()))
                     {
                         ResultsUserAlreadyHas += NumberOfQuestions(theModel, db);
                     }
                 }
                 int SolutionsToAdd = results.Length - ResultsUserAlreadyHas;
                 if (SolutionsToAdd <= theUser.Credit)
                 {
                     theUser.Credit -= SolutionsToAdd;
                     AddSolution(theUser, model, db);
                     db.SubmitChanges();
                 }
                 else
                 {
                     return false;
                 }
             }
         }
         else
         {
             IQueryable<Question> retrieved = from theAnswers in db.Questions
                                              where theAnswers.Textbook_Title.Equals(model.Textbook)
                                              && theAnswers.Unit_Title.Equals(model.Unit)
                                              && theAnswers.Chapter_Title.Equals(model.Chapter)
                                              && theAnswers.Section_Title.Equals(model.Section)
                                              && theAnswers.Page_Number.Equals(model.Page)
                                              select theAnswers;
             Question[] results = retrieved.ToArray<Question>();
             int ResultsUserAlreadyHas = 0;
             foreach (Question theQuestion in results)
             {
                 SelectModel theModel = new SelectModel(theQuestion);
                 if (UserHasAccess(theUser.UserName, theModel.ToString()))
                 {
                     ResultsUserAlreadyHas += NumberOfQuestions(theModel, db);
                 }
             }
             int SolutionsToAdd = results.Length - ResultsUserAlreadyHas;
             if (SolutionsToAdd <= theUser.Credit)
             {
                 theUser.Credit -= SolutionsToAdd;
                 AddSolution(theUser, model, db);
                 db.SubmitChanges();
             }
             else
             {
                 return false;
             }
         }
     }
     else
     {
         IQueryable<Question> retrieved = from theAnswers in db.Questions
                                          where theAnswers.Textbook_Title.Equals(model.Textbook)
                                          && theAnswers.Unit_Title.Equals(model.Unit)
                                          && theAnswers.Chapter_Title.Equals(model.Chapter)
                                          && theAnswers.Section_Title.Equals(model.Section)
                                          && theAnswers.Page_Number.Equals(model.Page)
                                          && theAnswers.Question_Number.Equals(model.Question)
                                          select theAnswers;
         Question[] results = retrieved.ToArray<Question>();
         int ResultsUserAlreadyHas = 0;
         foreach (Question theQuestion in results)
         {
             SelectModel theModel = new SelectModel(theQuestion);
             if (UserHasAccess(theUser.UserName, theModel.ToString()))
             {
                 ResultsUserAlreadyHas += NumberOfQuestions(theModel, db);
             }
         }
         int SolutionsToAdd = results.Length - ResultsUserAlreadyHas;
         if (SolutionsToAdd <= theUser.Credit)
         {
             theUser.Credit -= SolutionsToAdd;
             AddSolution(theUser, model, db);
             db.SubmitChanges();
         }
         else
         {
             return false;
         }
     }
     return true;
 }