コード例 #1
0
        public ActionResult Index()
        {
            var login   = Models.Authorization.Login;
            var user    = db.Accounts.FirstOrDefault(m => m.Login == login) as User;
            var history = HistoryViewModel.CreateHistory(db, DateTime.Now.AddDays(-7), DateTime.Now, user);

            IEnumerable <int> watchedLessons = user.FinishedLessons.Select(m => m.Id);

            var courses = db.Courses.ToList().Select(
                m => m.ToViewModel(
                    m.Lessons.Select(
                        lesson => lesson.ToViewModel(watchedLessons.Any(watchedId => watchedId == lesson.Id)))));
            //                    ^^^^^^^^^^^^^^^^^| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            //                          (1)                                       (2)
            // (1): lesson.ToViewModel(bool isWatched). Creating ViewModel from Enttiy model using extension. Marks it watched/unwatched
            // (2): Looking to watchedLessons and returns true if we've found current LessonId

            var model = new AccountViewModel()
            {
                Account = user,
                History = history,
                Courses = courses
            };

            return(View(model));
        }