Exemple #1
0
        /// <summary>
        /// Get all movies list from database
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sortingOrder"></param>
        /// <param name="sortingDir"></param>
        /// <param name="searchText"></param>
        /// <returns></returns>
        public MovieModel GetAllMovies(MovieModel model, string sortingOrder, string sortingDir, string searchText)
        {
            var result = from m in db.tblMovies select new MovieModel {
                MovieName = m.MovieName
            };

            if (!string.IsNullOrEmpty(searchText))
            {
                result = from m in db.tblMovies
                         join p in db.tblProducers on m.Producer equals p.ProducerID
                         where m.MovieName.ToLower().Contains(searchText.ToLower()) ||
                         p.FirstName.ToLower().Contains(searchText.ToLower()) ||
                         p.LastName.ToLower().Contains(searchText.ToLower()) ||
                         m.genre.ToLower().Contains(searchText.ToLower()) ||
                         m.title.ToLower().Contains(searchText.ToLower())
                         select new MovieModel
                {
                    MovieName    = m.MovieName,
                    Year         = (long)m.Year,
                    Producer     = (long)m.Producer,
                    ProducerName = p.FirstName + " " + p.LastName,
                    Genre        = m.genre,
                    Title        = m.title,
                    MovieID      = m.MovieID
                };
            }
            else
            {
                result = from m in db.tblMovies
                         join p in db.tblProducers on m.Producer equals p.ProducerID
                         select new MovieModel
                {
                    MovieName    = m.MovieName,
                    Year         = (long)m.Year,
                    Producer     = (long)m.Producer,
                    ProducerName = p.FirstName + " " + p.LastName,
                    Genre        = m.genre,
                    Title        = m.title,
                    MovieID      = m.MovieID
                };
            }

            model.MovieWithProducer = result.ToList();

            switch (sortingOrder)
            {
            case "MovieName":
                if (sortingDir.ToLower().Equals("asc"))
                {
                    model.MovieWithProducer = model.MovieWithProducer.OrderBy(u => u.MovieName).ToList();
                }
                else
                {
                    model.MovieWithProducer = model.MovieWithProducer.OrderByDescending(u => u.MovieName).ToList();
                }
                break;

            case "Year":
                if (sortingDir.ToLower().Equals("asc"))
                {
                    model.MovieWithProducer = model.MovieWithProducer.OrderBy(u => u.Year).ToList();
                }
                else
                {
                    model.MovieWithProducer = model.MovieWithProducer.OrderByDescending(u => u.Year).ToList();
                }
                break;

            case "ProducerName":
                if (sortingDir.ToLower().Equals("asc"))
                {
                    model.MovieWithProducer = model.MovieWithProducer.OrderBy(u => u.ProducerName).ToList();
                }
                else
                {
                    model.MovieWithProducer = model.MovieWithProducer.OrderByDescending(u => u.ProducerName).ToList();
                }
                break;

            case "Genre":
                if (sortingDir.ToLower().Equals("asc"))
                {
                    model.MovieWithProducer = model.MovieWithProducer.OrderBy(u => u.Genre).ToList();
                }
                else
                {
                    model.MovieWithProducer = model.MovieWithProducer.OrderByDescending(u => u.Genre).ToList();
                }
                break;

            case "title":
                if (sortingDir.ToLower().Equals("asc"))
                {
                    model.MovieWithProducer = model.MovieWithProducer.OrderBy(u => u.Title).ToList();
                }
                else
                {
                    model.MovieWithProducer = model.MovieWithProducer.OrderByDescending(u => u.Title).ToList();
                }
                break;

            default:
                model.MovieWithProducer = model.MovieWithProducer.OrderByDescending(u => u.MovieID).ToList();
                break;
            }



            return(model);
        }