예제 #1
0
        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                UserProfile userProfile = await accessors.UserProfileAccesor.GetAsync(turnContext, () => new UserProfile());

                if (string.IsNullOrEmpty(userProfile.Language))
                {
                    userProfile.Language = TranslatorHelper.GetDesiredLanguage(turnContext.Activity.Text);
                    await accessors.UserProfileAccesor.SetAsync(turnContext, userProfile);

                    await accessors.UserState.SaveChangesAsync(turnContext);

                    var confirmedLanguageResponse = await TranslatorHelper.TranslateSentenceAsync("Está hecho, tu lenguaje está seleccionado", userProfile.Language);

                    await turnContext.SendActivityAsync(confirmedLanguageResponse);
                }
                else
                {
                    if (turnContext.Activity.Text == "nuevo idioma")
                    {
                        await turnContext.SendActivityAsync("Solo dí la palabra mágica");

                        userProfile.Language = string.Empty;
                        await accessors.UserProfileAccesor.SetAsync(turnContext, userProfile);

                        await accessors.UserState.SaveChangesAsync(turnContext);
                    }
                    else
                    {
                        Attachment plAttachment = new Attachment()
                        {
                            ContentType = HeroCard.ContentType,
                            Content     = await MoviesHelper.SearchMovieAsync(turnContext.Activity.Text, userProfile.Language),
                        };

                        var reply = turnContext.Activity.CreateReply();
                        reply.Attachments.Add(plAttachment);
                        await turnContext.SendActivityAsync(reply);
                    }
                }
            }
            else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
            {
                if (turnContext.Activity.MembersAdded != null)
                {
                    await SendWelcomeMessageAsync(turnContext, cancellationToken);
                }
            }
            else
            {
                await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected", cancellationToken : cancellationToken);
            }
        }
예제 #2
0
 /// <summary>
 /// GetDeserializedMovies
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public List <Movie> GetDeserializedMovies(string obj)
 {
     try
     {
         string       jsonstring   = JsonConvert.SerializeObject(obj);
         var          stringObject = JsonConvert.DeserializeObject <string>(jsonstring);
         MoviesHelper moviesHelper = JsonConvert.DeserializeObject <MoviesHelper>(stringObject);
         return(moviesHelper.Movies);
     }
     catch (Exception ex)
     {
         logger.Error(ex, "Ëxception Occured while JsonConversion");
     }
     return(new List <Movie>());
 }
        static void Main(string[] args)
        {
            #region HOMEWORK LINQ

            List <Movie> allmovies = MoviesHelper.GetAllActionMovies();

            List <Movie> movies = MoviesHelper.MyMovies(allmovies);

            // 1 Find all movies that their titles starts with "L"

            var StartLLambda = movies.Where(m => m.Title.StartsWith("L")).ToList();

            StartLLambda.ForEach(m => Console.WriteLine(m.Title));
            if (StartLLambda.Count == 0)
            {
                Console.WriteLine("We don't have film starting with L");
            }

            var StartLQuery = (from movie in movies where movie.Title.StartsWith("L") select movie).ToList();

            // 2 Find the NUMBER of movies that have rating higher than 7.5

            var numLambda = movies.Where(m => m.Rating > 7.5f).ToList().Count;

            var numQuery = (from movie in movies where movie.Rating > 7.5f select movie).ToList().Count;
            Console.WriteLine("Number of movies with rating higher than 7.5 is " + numQuery);

            // 3 Find all movies TITLES with year of production before 2005

            var before2005Lambda = movies.Where(m => m.Year < 2005).Select(m => m.Title).ToList();
            before2005Lambda.ForEach(m => Console.WriteLine(m));

            var before2005Query = (from m in movies where m.Year < 2005 select m.Title).ToList();
            //before2005Query.ForEach(m => Console.WriteLine(m));

            // 4 Find all movies TITLES and RATING that have rating higher then 8.0

            var tilesAndRatingLambda = movies.Where(m => m.Rating > 8.0f)
                                       .Select(m => new { Naslov = m.Title, Rejting = m.Rating }).ToList();

            tilesAndRatingLambda.ForEach(m => Console.WriteLine(m));

            var tilesAndRatingQuery = (from movie in movies where movie.Rating > 8.0f select movie).
                                      Select(m => new { Naslov = m.Title, Rejting = m.Rating }).ToList();

            tilesAndRatingQuery.ForEach(m => Console.WriteLine(m));

            // 5 Find first 5 movies that have duration time longer then 2 hours

            var first5longer120Lambda = movies.Where(m => m.Duration > 120).Take(5).ToList();
            //first5longer120Lambda.ForEach(m => Console.WriteLine(m.Title));

            var first5longer120Query = (from movie in movies where movie.Duration > 120 select movie).Take(5).ToList();
            first5longer120Query.ForEach(m => Console.WriteLine(m.Title));

            // 6 Find last 3 movies TITLES and DURATION that have duration less then 2 hours

            var last3less120Lambda = movies.Where(m => m.Duration < 120)
                                     .TakeLast(3).Select(m => new { m.Title, m.Duration }).ToList();


            //last3less120Lambda.ForEach(m => Console.WriteLine(m));

            var last3less120Query = (from movie in movies where movie.Duration < 120 select movie)
                                    .Select(m => new { m.Title, m.Duration }).TakeLast(3).ToList();

            last3less120Query.ForEach(m => Console.WriteLine(m));


            // 7 Find all movies TITLES and RATING and order them by DURATION (DESC) - no condition needed

            var descDurationLambda = movies.OrderByDescending(d => d.Duration).Select(m => new { m.Title, m.Rating }).ToList();


            var descDurationQuery = (from movie in movies orderby movie.Duration descending select movie)
                                    .Select(m => new { m.Title, m.Rating }).ToList();


            descDurationQuery.ForEach(m => Console.WriteLine(m));


            // 8 Find all movies with TITLES that don't start with A and TITLES include more than 7 characters

            var char7notstartA = movies.Where(m => m.Title.Length > 7 && !m.Title.StartsWith('A')).ToList();


            var char7notstartAQuery = (from movie in movies
                                       where movie.Title.Length > 7 && !movie.Title.StartsWith('A')
                                       select movie).ToList();

            char7notstartAQuery.ForEach(m => Console.WriteLine(m.Title));


            // 9 Find all movies RATINGS that have RATINGS higher than 7.2, DURATIONS less then 1hour and 50min

            var ratingsLambda = movies.Where(m => m.Rating > 7.2f && m.Duration < 110).Select(m => m.Rating).ToList();

            var ratingsQuery = (from movie in movies
                                where movie.Rating > 7.2f && movie.Duration < 110
                                select movie.Rating).ToList();



            ratingsQuery.ForEach(m => Console.WriteLine(m));

            // 10 Find all movies TITLES and RATINGS that have TITLES with less than 10 characters, DURATION
            //longer than 2 hours, RATING higher then 6.7 and order them by TITLE(ASC)

            var lastCondition = movies.Where(m => m.Title.Length < 10 && m.Duration > 120 && m.Rating > 6.7f)
                                .OrderBy(t => t.Title).Select(m => new { Title = m.Title, Rating = m.Rating }).ToList();


            var lastConditionQuery = (from m in movies
                                      where m.Title.Length < 10 && m.Duration > 120 && m.Rating > 6.7f
                                      orderby m.Title select m)
                                     .Select(m => new { Title = m.Title, Rating = m.Rating }).ToList();

            Console.ForegroundColor = ConsoleColor.DarkGreen;
            lastConditionQuery.ForEach(m => Console.WriteLine(m));


            #endregion

            Console.ReadLine();
        }
예제 #4
0
        static void Main(string[] args)
        {
            #region FOREACH with LINQ

            var partOfMovies = new List <Movie>()
            {
                new Movie()
                {
                    Title = "The Mountain II", Year = 2016, Rating = 8.9f, Duration = 135
                },
                new Movie()
                {
                    Title = "Seven Samurai", Year = 1954, Rating = 8.6f, Duration = 207
                },
                new Movie()
                {
                    Title = "Big Hero 6", Year = 2014, Rating = 7.8f, Duration = 102
                },
            };

            //foreach (var movie in partOfMovies)
            //{
            //    Console.WriteLine(movie.Title);
            //}

            // LAMBDA syntax in C#
            // (par => expression)

            //Console.ForegroundColor = ConsoleColor.Red;
            //partOfMovies.ForEach(movie => Console.WriteLine(movie.Title));
            //Console.ResetColor();
            #endregion


            #region Iterating Dictionary<TKey,TValue> with FOR using LINQ

            //var moviesCodes = new Dictionary<int, Movie>()
            //{
            //    {1,  new Movie(){ Title = "The Mountain II", Year = 2016, Rating = 8.9f, Duration = 135 } },
            //    {2,  new Movie(){ Title = "Seven Samurai", Year = 1954, Rating = 8.6f, Duration = 207 } },
            //    {3,  new Movie(){ Title = "Big Hero 6", Year = 2014, Rating = 7.8f, Duration = 102 } }
            //};

            //for (int i = 0; i < moviesCodes.Count; i++)
            //{
            //    KeyValuePair<int, Movie> movie = moviesCodes.ElementAt(i);
            //    Console.WriteLine($"Key: {movie.Key}, Value: {movie.Value.Title}");
            //}

            #endregion


            #region LINQ in Action - EXAMPLES

            List <Movie> movies = MoviesHelper.GetAllActionMovies();

            // 1. Find ALL movies with Title starting with 'T'
            //Lambda syntax
            var allMoviesTitleStartingWithTLambda = movies
                                                    .Where(movie => movie.Title.StartsWith('T')).ToList();

            //Sql query syntax
            var allMoviesTitleStartingWithTQuery = (from movie in movies
                                                    where movie.Title.StartsWith('T')
                                                    select movie.Title).ToList();

            //foreach (var movie in allMoviesTitleStartingWithTLambda)
            //{
            //    Console.WriteLine(movie.Title);
            //}

            // Single
            // SingleOrDefault
            // First
            // FirstOrDefault
            // Last
            // LastOrDefault

            // 2. Find the FIRST Movie with Title starting with 'G'
            //var firstMovieTitleWithG = movies.Single(movie => movie.Title.StartsWith('G'));
            //var firstMovieTitleWithG = movies.SingleOrDefault(movie => movie.Title.StartsWith('G'));
            //var firstMovieTitleWithG = movies.First(movie => movie.Title.StartsWith('G'));
            var firstMovieTitleWithGLambda = movies.FirstOrDefault(movie => movie.Title.StartsWith('G'));

            var firstMovieTitleWithGQuery = (from movie in movies
                                             where movie.Title.StartsWith('G')
                                             select movie.Title).FirstOrDefault();

            if (firstMovieTitleWithGLambda == null)
            {
                Console.WriteLine("Movie starting with G not exists!!");
            }

            //Console.WriteLine("The application continues....");

            //Console.WriteLine(firstMovieTitleWithG.Title);

            // 3. Find the LAST Movie with Title starting with 'G'

            var lastMovieStartingWithG = movies.Last(movie => movie.Title.StartsWith('G'));

            Console.WriteLine("Last movie starting with G: " + lastMovieStartingWithG.Title);

            // 4. Find ALL movies TITLES that plays for more than 2 hours

            // Miki da si naprai pukanki :)))))
            var allMoviesTitlesLongerThan2Hours = movies
                                                  .Where(movie => movie.Duration > 120)
                                                  .Select(movie => movie.Title)
                                                  .ToList();

            allMoviesTitlesLongerThan2Hours.ForEach(title => Console.WriteLine(title));

            //foreach (var title in allMoviesTitlesLongerThan2Hours)
            //    Console.WriteLine(title);


            // 4 Extended. Find ALL movies TITLES AND RATINGS that plays for more than 2 hours
            // new { Something = movie.Title, NestoNesto = movie.Duration}
            var allMoviesTitlesRatingsLongerThan2Hours = movies
                                                         .Where(movie => movie.Duration > 5000)
                                                         .Where(movie => movie.Year < 2010)
                                                         .Select(movie => new { Title = movie.Title, Rating = movie.Rating })
                                                         .ToList();

            var querySyntax = (from movie in movies
                               where movie.Duration > 120
                               where movie.Year < 2010
                               select movie);

            var querySyntaxOneLine = from movie in movies
                                     where movie.Duration > 120 && movie.Year < 2010
                                     select movie;

            if (allMoviesTitlesRatingsLongerThan2Hours.Count() > 0)
            {
                // code here if there are records from WHERE :)))
            }

            foreach (var item in allMoviesTitlesRatingsLongerThan2Hours)
            {
                Console.WriteLine(item.Title);
            }

            // 5. Find ALL movies TITLES produced before 2010 and ORDER by Title

            // 6. Find ALL movies TITLES produced after 2010, with Rating over 8.5 and ORDER by Title (Ascending/Descending Order)

            var allMoviesTitlesComplexConditionAsc = movies
                                                     .Where(movie => movie.Year > 2010)
                                                     .Where(movie => movie.Rating > 8.5)
                                                     .Select(movie => movie.Title)
                                                     //.OrderBy(title => title)
                                                     .OrderByDescending(title => title)
                                                     .ToList();

            // 7. What is the AVERAGE RANKING of ALL movies
            var averageRating = movies.Average(movie => movie.Rating);

            Console.WriteLine(averageRating);

            // 8. Find the BEST RANKING movie title (get only the TITLE and RANKING)

            var bestRankingofTheMovie = movies.Max(movie => movie.Rating);
            var bestTitle             = movies
                                        .Single(movie => movie.Rating == bestRankingofTheMovie)
                                        .Title;
            //.Select(movie => movie.Title)
            //.Single();

            // 9. Find the WORST RANKING movie (get only the TITLE and DURATION)
            var worstMovieRanking = movies.Min(movie => movie.Rating);

            // 10. Get FIRST five movies
            var firstFive = movies.Take(5).ToList();

            // 11. Get LAST five movies
            var lastFive = movies.TakeLast(5).ToList();

            // 12. Get from the third one to the end of the list
            var customRange         = movies.Skip(3).ToList();
            var customRangeNextFive = movies.Skip(3)
                                      .Take(5)
                                      .ToList();

            // Homework:
            // part 1 - add additional 10 movies
            // part 2 - you will get another 10 requirements regarding the same LINQ methods :)))

            #endregion

            Console.ReadLine();
        }
예제 #5
0
        static void Main(string[] args)
        {
            var movies = MoviesHelper.GetAllKindOfMovies();

            //1.    Find all movies that their titles starts with "L"
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Movies that their titles starts with \"L\"");
            Console.ResetColor();

            //Lambda syntax
            var moviesTitlesStartsWithL = movies
                                          .Where(movie => movie.Title.StartsWith('L'))
                                          .ToList();

            moviesTitlesStartsWithL.ForEach(movie => Console.WriteLine(movie.Title));

            //Query syntax
            Console.WriteLine("------------------------------------------");
            var moviesTitlesStartsWithL2 = (from movie in movies
                                            where movie.Title.StartsWith('L')
                                            select movie)
                                           .ToList();

            moviesTitlesStartsWithL2.ForEach(movie => Console.WriteLine(movie.Title));

            Console.WriteLine("------------------------------------------");


            //2.    Find the NUMBER of movies that have rating higher than 7.5
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("NUMBER of movies that have rating higher than 7.5");
            Console.ResetColor();

            //Lambda syntax
            var moviesWithHigherRating = movies
                                         .Where(movie => movie.Rating > 7.5)
                                         .ToList()
                                         .Count();

            Console.WriteLine(moviesWithHigherRating);

            //Query syntax
            Console.WriteLine("------------------------------------------");
            var moviesWithHigherRating2 = (from movie in movies
                                           where movie.Rating > 7.5
                                           select movie)
                                          .ToList()
                                          .Count();

            Console.WriteLine(moviesWithHigherRating2);
            Console.WriteLine("------------------------------------------");

            //3.    Find all movies TITLES with year of production before 2005
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("All movies TITLES with year of production before 2005");
            Console.ResetColor();

            //Lambda syntax
            //Query syntax
            var moviesProducedBefore2005 = movies
                                           .Where(movie => movie.Year < 2005)
                                           .Select(movie => movie.Title)
                                           .ToList();

            moviesProducedBefore2005.ForEach(title => Console.WriteLine(title));

            //Query syntax
            Console.WriteLine("------------------------------------------");
            var moviesProducedBefore2005_2 = (from movie in movies
                                              where movie.Year < 2005
                                              select movie.Title)
                                             .ToList();

            moviesProducedBefore2005_2.ForEach(title => Console.WriteLine(title));
            Console.WriteLine("------------------------------------------");


            //4.    Find all movies TITLES and RATING that have rating higher then 8.0
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("All movies TITLES and RATING that have rating higher then 8.0");
            Console.ResetColor();

            //Lambda syntax
            var titlesAndRatingsOfHighRatedMovies = movies
                                                    .Where(movie => movie.Rating > 8.0)
                                                    .Select(movie => new { movie.Title, movie.Rating })
                                                    .ToList();

            titlesAndRatingsOfHighRatedMovies.ForEach(movie => Console.WriteLine($"Title: {movie.Title}, Rating: {movie.Rating}"));

            //Query syntax
            Console.WriteLine("------------------------------------------");
            var titlesAndRatingsOfHighRatedMovies2 = (from movie in movies
                                                      where movie.Rating > 8.0
                                                      select new { movie.Title, movie.Rating })
                                                     .ToList();

            titlesAndRatingsOfHighRatedMovies2.ForEach(movie => Console.WriteLine($"Title: {movie.Title}, Rating: {movie.Rating}"));
            Console.WriteLine("------------------------------------------");

            //5.    Find first 5 movies that have duration time longer then 2 hours
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("First 5 movies that have duration time longer then 2 hours");
            Console.ResetColor();

            //Lambda syntax
            var moviesLongerThen2Hours = movies
                                         .Where(movie => movie.Duration > 120)
                                         .Take(5)
                                         .ToList();

            moviesLongerThen2Hours.ForEach(movie => Console.WriteLine(movie.Title));

            //Query syntax
            Console.WriteLine("------------------------------------------");
            var moviesLongerThen2Hours2 = (from movie in movies
                                           where movie.Duration > 120
                                           select movie)
                                          .Take(5)
                                          .ToList();

            moviesLongerThen2Hours2.ForEach(movie => Console.WriteLine(movie.Title));
            Console.WriteLine("------------------------------------------");


            //6.    Find last 3 movies TITLES and DURATION that have duration less then 2 hours
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Last 3 movies TITLES and DURATION that have duration less then 2 hours");
            Console.ResetColor();

            //Lambda syntax
            var moviesDurationLessThen2Hours = movies
                                               .Where(movie => movie.Duration < 120)
                                               .Select(movie => new { movie.Title, movie.Duration })
                                               .TakeLast(3)
                                               .ToList();

            moviesDurationLessThen2Hours.ForEach(movie => Console.WriteLine($"Title: {movie.Title}, Duration {movie.Duration}"));

            //Query syntax
            Console.WriteLine("------------------------------------------");
            var moviesDurationLessThen2Hours2 = (from movie in movies
                                                 where movie.Duration < 120
                                                 select new { movie.Title, movie.Duration })
                                                .TakeLast(3)
                                                .ToList();

            moviesDurationLessThen2Hours2.ForEach(movie => Console.WriteLine($"Title: {movie.Title}, Duration {movie.Duration}"));
            Console.WriteLine("------------------------------------------");

            //7.    Find all movies TITLES and RATING and order them by DURATION(DESC) -no condition needed
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("All movies TITLES and RATING and order them by DURATION(DESC)");
            Console.ResetColor();

            //Lambda syntax
            var allMoviesTitlesAndRatings = movies
                                            .OrderByDescending(movie => movie.Duration)
                                            .Select(movie => new { movie.Title, movie.Rating })
                                            .ToList();

            allMoviesTitlesAndRatings.ForEach(movie => Console.WriteLine($"Title: {movie.Title}, Rating: {movie.Rating}"));

            //Query syntax
            Console.WriteLine("------------------------------------------");
            var allMoviesTitlesAndRatings2 = (from movie in movies
                                              orderby movie.Duration descending
                                              select movie)
                                             .Select(movie => new { movie.Title, movie.Rating })
                                             .ToList();

            allMoviesTitlesAndRatings2.ForEach(movie => Console.WriteLine($"Title: {movie.Title}, Rating: {movie.Rating}"));
            Console.WriteLine("------------------------------------------");

            //8.    Find all movies with TITLES that don't start with A and TITLES include more than 7 characters
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("All movies with TITLES that don't start with A and TITLES include more than 7 characters");
            Console.ResetColor();

            //Lambda syntax
            var moviesTitlesWith7Char = movies
                                        .Where(movie => !movie.Title.StartsWith('A'))
                                        .Where(movie => movie.Title.Count() > 7)
                                        .Select(movie => movie.Title)
                                        .ToList();

            moviesTitlesWith7Char.ForEach(title => Console.WriteLine(title));

            //Query syntax
            Console.WriteLine("------------------------------------------");
            var moviesTitlesWith7Char2 = (from movie in movies
                                          where !movie.Title.StartsWith('A') && movie.Title.Count() > 7
                                          select movie.Title)
                                         .ToList();

            moviesTitlesWith7Char2.ForEach(title => Console.WriteLine(title));
            Console.WriteLine("------------------------------------------");

            //9.    Find all movies RATINGS that have RATINGS higher than 7.2, DURATIONS less then 1hour
            //    and 50min
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("All movies RATINGS that have RATINGS higher than 7.2, DURATIONS less then 1hour and 50min");
            Console.ResetColor();

            //Lambda syntax
            var moviesRatings = movies
                                .Where(movie => movie.Rating > 7.2 && movie.Duration < 110)
                                .Select(movie => movie.Rating)
                                .ToList();

            moviesRatings.ForEach(rating => Console.WriteLine(rating));

            //Query syntax
            Console.WriteLine("------------------------------------------");
            var moviesRatings2 = (from movie in movies
                                  where movie.Rating > 7.2 && movie.Duration < 110
                                  select movie.Rating)
                                 .ToList();

            moviesRatings2.ForEach(rating => Console.WriteLine(rating));
            Console.WriteLine("------------------------------------------");


            //10.    Find all movies TITLES and RATINGS that have TITLES with less than 10 characters, DURATION
            //    longer than 2 hours, RATING higher then 6.7 and order them by TITLE(ASC)
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("All movies TITLES and RATINGS that have TITLES with less than 10 characters, " +
                              "DURATION longer than 2 hours, RATING higher then 6.7 and order them by TITLE(ASC)");
            Console.ResetColor();

            //Lambda syntax
            var moviesTitlesAndRatings = movies
                                         .Where(movie => movie.Title.Count() < 10)
                                         .Where(movie => movie.Duration > 120)
                                         .Where(movie => movie.Rating > 6.7)
                                         .OrderBy(movie => movie.Title)
                                         .Select(movie => new { movie.Title, movie.Rating })
                                         .ToList();

            moviesTitlesAndRatings.ForEach(movie => Console.WriteLine($"Title: {movie.Title}, Rating: {movie.Rating}"));

            //Query syntax
            Console.WriteLine("------------------------------------------");
            var moviesTitlesAndRatings2 = (from movie in movies
                                           where movie.Title.Count() < 10
                                           where movie.Duration > 120
                                           where movie.Rating > 6.7
                                           orderby movie.Title
                                           select movie)
                                          .Select(movie => new { movie.Title, movie.Rating })
                                          .ToList();

            moviesTitlesAndRatings2.ForEach(movie => Console.WriteLine($"Title: {movie.Title}, Rating: {movie.Rating}"));
            Console.WriteLine("------------------------------------------");
        }
예제 #6
0
        static void Main(string[] args)
        {
            List <Movie> movies = MoviesHelper.MyMovies();



            // ****** Find all movies that their titles starts with "L"

            var startingWith_T = movies
                                 .Where(movie => movie.Title.StartsWith('L'))
                                 .ToList();


            //startingWith_T.ForEach(movie => Console.WriteLine(movie.Title));

            // ***** Find the NUMBER of movies that have rating higher than 7.5

            var moviesRatingHigher75 = movies
                                       .Where(movie => movie.Rating > 7.5f)
                                       .ToList();

            //moviesRatingHigher75.ForEach(movie => Console.WriteLine(movie.Title));

            // ****** Find all movies TITLES with year of production before 2005

            var movieBefore2005 = movies
                                  .Where(movie => movie.Year < 2005)
                                  .ToList();

            // movieBefore2005.ForEach(movie => Console.WriteLine(movie.Title));


            // ***** Find all movies TITLES and RATING that have rating higher then 8.0

            var moviesRatingHigher80 = movies
                                       .Where(movie => movie.Rating > 8.0f)
                                       .ToList();

            //moviesRatingHigher80.ForEach(movie => Console.WriteLine(movie.Title));


            // ***** Find first 5 movies that have duration time longer then 2 hours

            var first5MoviesLongerthen120 = movies
                                            .Where(movie => movie.Duration > 120)
                                            .Take(5)
                                            .ToList();
            //first5MoviesLongerthen120.ForEach(movie => Console.WriteLine(movie.Title));



            // ****** Find last 3 movies TITLES and DURATION that have duration less then 2 hours

            var last3Moviesless120 = movies
                                     .Where(movie => movie.Duration < 120)
                                     .TakeLast(3)
                                     .ToList();
            //last3Moviesless120.ForEach(movie => Console.WriteLine(movie));


            // ***** Find all movies TITLES and RATING and order them by DURATION(DESC) -no condition needed

            var allTitlesRatingDesc = movies
                                      .OrderByDescending(movie => movie.Duration)
                                      .Select(movie => new { movie.Title, movie.Rating })
                                      .ToList();
            //allTitlesRatingDesc.ForEach(movie => Console.WriteLine(movie));

            // ****** Find all movies with TITLES that don't start with A and TITLES include more than 7 characters

            var titlesDontStartAmore7 = movies
                                        .Where(movie => !movie.Title.StartsWith('A'))
                                        .Where(movie => movie.Title.Count() > 7)
                                        .ToList();
            //titlesDontStartAmore7.ForEach(movie => Console.WriteLine(movie.Title));


            // ****** Find all movies RATINGS that have RATINGS higher than 7.2, DURATIONS less then 1hour and 50min

            var allRatings72Durationsless110 = movies
                                               .Where(movie => movie.Rating > 7.2f)
                                               .Where(movie => movie.Duration < 110)
                                               .ToList();
            //allRatings72Durationsless110.ForEach(movie => Console.WriteLine(movie.Title));


            // ****** Find all movies TITLES and RATINGS that have TITLES with less than 10 characters, DURATION
            //        longer than 2 hours, RATING higher then 6.7 and order them by TITLE(ASC)

            var charactersDuranionRatingTitle = movies
                                                .Where(movie => movie.Title.Count() < 10)
                                                .Where(movie => movie.Duration > 120)
                                                .Where(movie => movie.Rating > 6.7f)
                                                .OrderBy(movie => movie.Title)
                                                .ToList();

            //charactersDuranionRatingTitle.ForEach(movie => Console.WriteLine(movie.Title));



            Console.ReadLine();
        }
예제 #7
0
        static void Main(string[] args)
        {
            #region FOREACH with LINQ

            var partOfMovies = new List <Movie>()
            {
                new Movie()
                {
                    Title = "The Mountain II", Year = 2016, Rating = 8.9f, Duration = 135
                },
                new Movie()
                {
                    Title = "Seven Samurai", Year = 1954, Rating = 8.6f, Duration = 207
                },
                new Movie()
                {
                    Title = "Big Hero 6", Year = 2014, Rating = 7.8f, Duration = 102
                },
            };

            partOfMovies.ForEach(m => Console.WriteLine(m.Title));

            #endregion


            #region Iterating Dictionary<TKey,TValue> with FOR using LINQ

            var moviesCodes = new Dictionary <int, Movie>()
            {
                { 1, new Movie()
                  {
                      Title = "The Mountain II", Year = 2016, Rating = 8.9f, Duration = 135
                  } },
                { 2, new Movie()
                  {
                      Title = "Seven Samurai", Year = 1954, Rating = 8.6f, Duration = 207
                  } },
                { 3, new Movie()
                  {
                      Title = "Big Hero 6", Year = 2014, Rating = 7.8f, Duration = 102
                  } }
            };

            for (int i = 0; i < moviesCodes.Count; i++)
            {
                KeyValuePair <int, Movie> movie = moviesCodes.ElementAt(i);
                Console.WriteLine($"Key: {movie.Key}, Value: {movie.Value.Title}");
            }

            #endregion


            #region LINQ in Action - EXAMPLES

            var movies = MoviesHelper.GetAllActionMovies();

            // 1. Find ALL movies with Title starting with 'T'

            // 2. Find the FIRST Movie with Title starting with 'G'

            // 3. Find the LAST Movie with Title starting with 'G'

            // 4. Find ALL movies TITLES that plays for more than 2 hours

            // 5. Find ALL movies TITLES produced before 2010 and ORDER by Title

            // 6. Find ALL movies TITLES produced after 2010, with Rating over 8.5 and ORDER by Title (Descending Order)

            // 7. What is the AVERAGE RANKING of ALL movies

            // 8. Find the BEST RANKING movie (get only the TITLE and RANKING)

            // 9. Find the WORST RANKING movie (get only the TITLE and DURATION)

            // 10. Get FIRST five movies

            // 11. Get LAST five movies

            #endregion

            Console.ReadLine();
        }
예제 #8
0
        static void Main(string[] args)
        {
            #region FOREACH with LINQ

            //var partOfMovies = new List<Movie>()
            //{
            //    new Movie(){ Title = "The Mountain II", Year = 2016, Rating = 8.9f, Duration = 135 },
            //    new Movie(){ Title = "Seven Samurai", Year = 1954, Rating = 8.6f, Duration = 207 },
            //    new Movie(){ Title = "Big Hero 6", Year = 2014, Rating = 7.8f, Duration = 102 },
            //};
            ////foreach (var movie in partOfMovies)
            ////{
            ////    Console.WriteLine(movie.Title);
            ////}
            //partOfMovies.ForEach(m => Console.WriteLine(m.Title));

            #endregion


            #region Iterating Dictionary<TKey,TValue> with FOR using LINQ

            var moviesCodes = new Dictionary <int, Movie>()
            {
                { 1, new Movie()
                  {
                      Title = "The Mountain II", Year = 2016, Rating = 8.9f, Duration = 135
                  } },
                { 2, new Movie()
                  {
                      Title = "Seven Samurai", Year = 1954, Rating = 8.6f, Duration = 207
                  } },
                { 3, new Movie()
                  {
                      Title = "Big Hero 6", Year = 2014, Rating = 7.8f, Duration = 102
                  } }
            };

            for (int i = 0; i < moviesCodes.Count; i++)
            {
                KeyValuePair <int, Movie> movie = moviesCodes.ElementAt(i);
                Console.WriteLine($"Key: {movie.Key}, Value: {movie.Value.Title}");
            }

            #endregion


            #region LINQ in Action - EXAMPLES

            var movies = MoviesHelper.GetAllActionMovies();

            // 1. Find ALL movies with Title starting with 'T'
            var allMoviesTitleStartWithT = movies
                                           .Where(movie => movie.Title.StartsWith('T')).ToList();
            var allMoviesWithTitleStartWithTQuery =
                (from movie in movies
                 where movie.Title.StartsWith('T')
                 select movie).ToList();
            foreach (var movie in allMoviesTitleStartWithT)
            {
                Console.WriteLine(movie.Title);;
            }
            {
            }
            // 2. Find the FIRST Movie with Title starting with 'G'
            var firstMovieTitleWithG = movies.FirstOrDefault(movie => movie.Title.StartsWith('E'));
            if (firstMovieTitleWithG == null)
            {
                Console.WriteLine("Movie start with G is not exist");
            }
            Console.WriteLine(firstMovieTitleWithG.Title);

            // 3. Find the LAST Movie with Title starting with 'G'
            var lastMovieStartingWithGLambda = movies.Last(movie => movie.Title.StartsWith('G'));
            Console.WriteLine("Last movie starting with G" + lastMovieStartingWithGLambda.Title);
            var firstMovieTitleWithQuery = (from movie in movies
                                            where movie.Title.StartsWith('G')
                                            select movie.Title).FirstOrDefault();
            if (lastMovieStartingWithGLambda == null)
            {
                Console.WriteLine("Movie start with G is not exist");
            }
            // 4. Find ALL movies TITLES that plays for more than 2 hours
            var allMoviesTitleLongerThenTwoOurs = movies
                                                  .Where(movie => movie.Duration > 120)
                                                  .Select(movie => movie.Title).ToList();
            allMoviesTitleLongerThenTwoOurs.ForEach(title => Console.WriteLine(title));
            foreach (var title in allMoviesTitleLongerThenTwoOurs)
            {
                Console.WriteLine(title);
            }
            // 4. Find ALL movies TITLES and Ratings that plays for more than 2 hours
            var allMoviesTitleAndRatingsLongerThenTwoOurs = movies
                                                            .Where(movie => movie.Duration > 120)
                                                            .Select(movie => new { naslov = movie.Title, Kvalitet = movie.Rating }).ToList();

            // 5. Find ALL movies TITLES produced before 2010 and ORDER by Title

            // 6. Find ALL movies TITLES produced after 2010, with Rating over 8.5 and ORDER by Title (Acessing/Descending Order)
            var allTitlesComplexConditionAcc = movies.Where(movie => movie.Year < 2010)
                                               .Where(movie => movie.Rating < 8.5)
                                               .OrderBy(movie => movie.Title)
                                               .Select(movie => movie.Title)
                                               .ToList();

            // 7. What is the AVERAGE RANKING of ALL movies
            var averageRating = movies.Average(movie => movie.Rating);

            // 8. Find the BEST RANKING movie (get only the TITLE and RANKING)
            var bastRankingMovie = movies.Max(movie => movie.Rating);
            var bestTitle        = movies.Single(movie => movie.Rating == bastRankingMovie);
            // 9. Find the WORST RANKING movie (get only the TITLE and DURATION)
            var worstMoviesRanking = movies.Min(movie => movie.Rating);
            // 10. Get FIRST five movies
            var firstFive = movies.Take(5).ToList();


            // 11. Get LAST five movies
            var lastFive = movies.TakeLast(5).ToList();
            #endregion

            Console.ReadLine();
        }
 public MovieController()
 {
     DbContext    = new ApplicationDbContext();
     MoviesHelper = new MoviesHelper(DbContext);
 }
예제 #10
0
        static void Main(string[] args)
        {
            List <Movie> movies = MoviesHelper.GetAllMovies();



            #region LINQ in Action - EXAMPLES


            // 1. Find ALL movies with Title starting with 'T'

            //Regular way
            foreach (var movie in movies)
            {
                if (movie.Title.StartsWith('T'))
                {
                    Console.WriteLine("Title starts with T: " + movie.Title);
                }
            }
            Console.WriteLine("Where() =============================================");


            //LINQ syntax: Using Where()

            List <Movie> moviesStartsWithT = movies.Where(x => x.Title.StartsWith('T')).ToList();
            foreach (var movie in moviesStartsWithT)
            {
                Console.WriteLine(movie.Title);
            }

            Console.WriteLine("First() =======================================");



            // 2. Find the FIRST Movie with Title starting with 'G'

            //Throws an exception because there is no movie that its Title starts with 'G'

            //Movie movieStartsWithG = movies.Where(x => x.Title.StartsWith('G')).First();
            //Console.WriteLine(movieStartsWithG.Title);


            Console.WriteLine("FirstOrDefault() ===============================");
            Movie movieStartsWithG = movies.Where(x => x.Title.StartsWith('G')).FirstOrDefault();

            //Needed to be handled with null check, because the default value for Movie is null
            if (movieStartsWithG != null)
            {
                Console.WriteLine(movieStartsWithG.Title);
            }
            else
            {
                Console.WriteLine("There is no such movie that its title starts with letter G");
            }



            // 3. Find the LAST Movie with Title starting with 'G'

            Console.WriteLine("Last() =============================================");
            //Movie lastStartsWithG = movies.Where(x => x.Title.StartsWith('G')).Last();
            //Console.WriteLine(lastStartsWithG.Title);


            Console.WriteLine("LastOrDefault() =============================================");
            Movie lastStartsWithG = movies.Where(x => x.Title.StartsWith('G')).LastOrDefault();

            Console.WriteLine(lastStartsWithG != null ? lastStartsWithG.Title : "No such movie");



            // 4. Find the FIRST and LAST movie with Raiting of 7.0

            Console.WriteLine("First() and Last() only =========================================");
            Movie raitingOfSeven = movies.First(x => x.Rating == 7);
            Console.WriteLine(raitingOfSeven != null ? raitingOfSeven.Title : "No movie with raiting of 7");

            Movie raitingOfSevenLast = movies.Last(x => x.Rating == 7);
            Console.WriteLine(raitingOfSeven != null ? raitingOfSeven.Title : "No movie with raiting of 7");



            // 5. Find the only (single) movie that its year is 2010

            Console.WriteLine("Single() and SingleOrDefault() only =========================================");
            //Movie onlyMovie = movies.Single(x => x.Year == 2010);
            //Console.WriteLine(onlyMovie != null ? onlyMovie.Title : "There are no movies of 2010 year, or there are more like that!");

            Movie onlyMovie = movies.SingleOrDefault(x => x.Year == 1995);
            Console.WriteLine(onlyMovie != null ? onlyMovie.Title : "There are no movies of 2010 year, or there are more like that!");



            // 4. Find ALL movies TITLES that plays for more than 2 hours

            Console.WriteLine("=========================================");
            List <Movie> longerThanTwoHours = movies.Where(x => x.Duration > 120).ToList();


            longerThanTwoHours.ForEach(x => Console.WriteLine(x.Title));
            //PrintAllElements(longerThanTwoHours);



            // 4.Extended: Find ALL movies TITLES that plays for more than 2 hours
            Console.WriteLine("Select() =========================================");
            List <string> movieTitlesLongerThanTwoHours = movies
                                                          .Where(x => x.Duration > 120)
                                                          .Select(x => x.Title)
                                                          .ToList();

            movieTitlesLongerThanTwoHours.ForEach(x => Console.WriteLine(x));


            var titleAndDurationOfLongerThanTwoHours = movies
                                                       .Where(x => x.Duration > 120)
                                                       .Select(x => new { Title = x.Title, Duration = x.Duration })
                                                       .ToList();

            titleAndDurationOfLongerThanTwoHours.ForEach(x => Console.WriteLine($"Title {x.Title} | Duration: {x.Duration}"));



            // 5. Find ALL movies TITLES produced before 2010 and ORDER by Title

            Console.WriteLine("OrderBy and OrderByDescending =====================================================");
            List <string> moviesBefore2010OrderdByTitle = movies
                                                          .Where(movie => movie.Year < 2010)
                                                          .Select(movie => movie.Title)
                                                          //.OrderBy(title => title)
                                                          .OrderByDescending(title => title)
                                                          .ToList();

            moviesBefore2010OrderdByTitle.ForEach(title => Console.WriteLine(title));


            // 6. Find ALL movies TITLES produced after 2010, with Rating over 8.5 and ORDER by Title (Descending Order)
            Console.WriteLine("Complex query ===============================================");
            List <string> moviesAfter2010 = movies.Where(x => x.Year > 2010 && x.Rating > 8.5)
                                            //.Where(x => x.Rating > 8.5)
                                            .Select(x => x.Title)
                                            .OrderByDescending(title => title)
                                            .ToList();

            moviesAfter2010.ForEach(title => Console.WriteLine(title));



            // 7. What is the AVERAGE RANKING of ALL movies
            Console.WriteLine("Operation methods: Average() | Sum() ===============================================");

            double averageRatingOfMovies = movies.Average(x => x.Rating);
            Console.WriteLine(averageRatingOfMovies);

            double totalMoviesRating = movies.Sum(x => x.Rating);
            Console.WriteLine("Total raiting of movies " + totalMoviesRating);



            // 8. Find the BEST RANKING movie (get only the TITLE and RANKING)

            Console.WriteLine("Max() ===============================================");

            double bestRank = movies.Max(x => x.Rating);

            var bestRankingMovie = movies
                                   .Where(movie => movie.Rating == bestRank)
                                   .Select(x => new { Title = x.Title, Raiting = x.Rating })
                                   .SingleOrDefault();
            Console.WriteLine($"Title: {bestRankingMovie.Title} | Raiting: {bestRankingMovie.Raiting}");



            // 9. Find the WORST RANKING movie (get only the TITLE and DURATION)

            Console.WriteLine("Max() ===============================================");

            double worstRanking = movies.Min(x => x.Rating);

            var worstRankginMovie = movies
                                    .Where(movie => movie.Rating == worstRanking)
                                    .Select(x => new { Title = x.Title, Duration = x.Duration })
                                    .SingleOrDefault();

            Console.WriteLine($"Title: {worstRankginMovie.Title} | Raiting: {worstRankginMovie.Duration}");



            // 10. Get FIRST five movies

            Console.WriteLine("Take() ===============================================");

            List <Movie> firstFiveMovies = movies.Take(5).ToList();
            firstFiveMovies.ForEach(x => Console.WriteLine("First five movies " + x.Title));



            // 11. Get LAST five movies

            Console.WriteLine("TakeLast() ===============================================");

            List <Movie> lastFiveMovies = movies.TakeLast(5).ToList();
            lastFiveMovies.ForEach(x => Console.WriteLine("Last five " + x.Title));



            // 12. Get from the third to the end of the list

            List <Movie> customRange = movies.Skip(5)
                                       .Take(6)
                                       .ToList();
            customRange.ForEach(x => Console.WriteLine("Second page " + x.Title));



            // =============================== BONUS ============================
            // ============================ BEFORE LINQ ============================
            // Typical SQL syntax

            //select Title
            //from Movies
            //where Raiting > 8.5
            var moviesWithHighRaiting = (from movie in movies
                                         where movie.Rating > 8.5
                                         select movie.Title).ToList();


            #endregion


            Console.ReadLine();
        }
예제 #11
0
        static void Main(string[] args)
        {
            var movies = MoviesHelper.GetAllActionMovies();

            // 1. Find ALL movies with Title starting with 'T'
            List <Movie> moviesStartingWithT = movies.Where(movie => movie.Title.StartsWith("T")).ToList();

            PrintMovies(moviesStartingWithT);

            // 2. Find the FIRST Movie with Title starting with 'G'
            //Movie task2 = movies.FirstOrDefault(movie => movie.Title[0] == 'G');
            Movie task2 = movies.FirstOrDefault(movie => movie.Title.StartsWith("G"));

            Console.WriteLine("---------------------------------");
            //if (task2 != null)
            //{
            //    Console.WriteLine(task2.GetInfo());
            //}
            //else
            //{
            //    Console.WriteLine("Nema takov film.");
            //}
            Console.WriteLine(task2 != null ? task2.GetInfo() : "Nema takov film vo listata");

            // 3. Find the LAST Movie with Title starting with 'G'
            Movie task3 = movies.LastOrDefault(movie => movie.Title.StartsWith("G"));

            Console.WriteLine("---------------------------------");
            Console.WriteLine(task3 != null ? task3.GetInfo() : "Nema takov film vo listata");

            // 4. Find ALL movies TITLES that plays for more than 2 hours
            List <string> movieNamesTwoHours = movies.Where(movie => movie.Duration > 120).Select(movie => movie.Title).ToList();

            Console.WriteLine("---------------------------------");
            movieNamesTwoHours.ForEach(movieName => Console.WriteLine(movieName));

            // 5. Find ALL movies TITLES produced before 2010 and ORDER by Title
            List <string> movieTitlesBefore2010 = movies.Where(movie => movie.Year < 2010)
                                                  .Select(movie => movie.Title)
                                                  .OrderBy(movie => movie)
                                                  .ToList();

            Console.WriteLine("---------------------------------");
            movieTitlesBefore2010.ForEach(movieName => Console.WriteLine(movieName));

            // 6. Find ALL movies TITLES produced after and in 2010, with Rating over 8.5 and ORDER by Title (Descending Order)

            List <string> producedAfter2010 = movies.Where(movie => movie.Year >= 2010 &&
                                                           movie.Rating > 8.5)
                                              .Select(movie => movie.Title)
                                              .OrderByDescending(movie => movie)
                                              .ToList();

            Console.WriteLine("---------------------------------");
            PrintMovies(producedAfter2010);

            // 7. What is the AVERAGE RANKING of ALL movies
            //double averageRanking = movies.Select(movie => movie.Rating).Average();
            double averageRanking = movies.Average(movie => movie.Rating);

            Console.WriteLine("---------------------------------");
            Console.WriteLine(averageRanking);

            // 8. Find the BEST RANKING movie (get only the TITLE and RANKING)
            //var task8 = movies.OrderByDescending(movie => movie.Rating).Select(movie => new { movie.Title, movie.Rating }).FirstOrDefault();

            //KeyValuePair<float, string> bestRankingMovie = movies.OrderByDescending(movie => movie.Rating)
            //    .Select(movie => new KeyValuePair<float, string>(movie.Rating, movie.Title)).FirstOrDefault();

            MovieSimpleModel bestRankingMovie = movies.OrderByDescending(movie => movie.Rating)
                                                .Select(movie => new MovieSimpleModel(movie.Title, movie.Rating)).FirstOrDefault();

            if (bestRankingMovie != null)
            {
                Console.WriteLine($"Best movie: {bestRankingMovie.Title} {bestRankingMovie.Rating}");
            }
            else
            {
                Console.WriteLine("Nema film vo listata");
            }

            // 9. Find the WORST RANKING movie (get only the TITLE and DURATION)
            KeyValuePair <string, int> task9 = movies.OrderBy(movie => movie.Rating)
                                               .Select(movie => new KeyValuePair <string, int>(movie.Title, movie.Duration))
                                               .FirstOrDefault();

            if (!string.IsNullOrEmpty(task9.Key) && task9.Value != 0)
            {
                Console.WriteLine($"Worst movie: {task9.Key} {task9.Value}");
            }
            else
            {
                Console.WriteLine("Nema film vo listata");
            }

            // 10. Get FIRST five movies
            List <Movie> firstFive = movies.Take(5).ToList();

            PrintMovies(firstFive);

            // 11. Get LAST five movies
            List <Movie> lastFive = movies.TakeLast(5).ToList();

            PrintMovies(lastFive);
        }
예제 #12
0
        static void Main(string[] args)
        {
            List <Movie> movies = MoviesHelper.GetAllMarvelMovies();

            #region 1
            ////* Find all movies that their titles starts with "L"

            //var allMoviesStartingWithL = movies.
            //                             Where(movie => movie.Title.StartsWith("L")).ToList();

            //foreach (var item in allMoviesStartingWithL)
            //{
            //    Console.WriteLine(item.Title);
            //}

            // Query Syntax

            //var querySyntaxOne = (from movie in movies
            //                      where movie.Title.StartsWith("L")
            //                      select movie.Title).ToList();

            //foreach (var item in querySyntaxOne)
            //{
            //    Console.WriteLine(item);
            //}
            #endregion
            //* Find the NUMBER of movies that have rating higher than 7.5
            #region 2
            //var moviesWithRatingHigher = movies.
            //                             Count(movie => movie.Rating >= 7.5f);

            // Query Syntax

            //var querySyntaxTwo = (from movie in movies
            //                      where movie.Rating >= 7.5
            //                      select movie.Rating).Count();


            //Console.WriteLine(querySyntaxTwo);


            //Console.WriteLine(moviesWithRatingHigher);
            #endregion
            //* Find all movies TITLES with year of production before 2005

            #region 3
            //var movieTitlesBefore2005 = movies.
            //                            Where(movie => movie.Year <= 2005).ToList();
            //foreach (var item in movieTitlesBefore2005)
            //{
            //    Console.WriteLine(item.Title);
            //}

            // Query Syntax

            //var querySyntaxThree = (from movie in movies
            //                        where movie.Year <= 2005
            //                        select movie.Title
            //    );
            //foreach (var item in querySyntaxThree)
            //{
            //    Console.WriteLine(item);
            //}

            #endregion

            #region 4
            //* Find all movies TITLES and RATING that have rating higher then 8.0

            //var movieTitlesAndRating = movies.
            //                            Where(movie => movie.Rating > 8.0).
            //                            Select(movie => new { Title = movie.Title, Rating = movie.Rating }).
            //                            ToList();

            //foreach (var item in movieTitlesAndRating)
            //{
            //    Console.WriteLine($"{item.Title} Rating:  {item.Rating}");
            //}

            // Query Syntax

            //var querySyntaxFour = (from movie in movies
            //                       where movie.Rating > 8.0
            //                       select movie.Title
            //                       ).ToList();

            //foreach (var item in querySyntaxFour)
            //{
            //    Console.WriteLine(item);
            //}
            #endregion
            #region 5


            //* Find first 5 movies that have duration time longer then 2 hours

            //var firstFiveMoviesLongerThan2Hours = movies.
            //                            Where(movie => movie.Duration > 120).
            //                            Take(5).ToList();
            //foreach (var item in firstFiveMoviesLongerThan2Hours)
            //{
            //    Console.WriteLine(item.Title);
            //}

            // Query Syntax

            //var querySyntaxFive = (from movie in movies
            //                       where movie.Duration > 120
            //                       select movie.Title)
            //                       .Take(5).ToList();
            //foreach (var item in querySyntaxFive)
            //{
            //    Console.WriteLine(item);
            //}
            #endregion
            #region 6

            //* Find last 3 movies TITLES and DURATION that have duration less then 2 hours

            //var lastThreeMovies = movies.
            //                        Where(movie => movie.Duration <= 120).
            //                        TakeLast(3).ToList();
            //foreach (var item in lastThreeMovies)
            //{
            //    Console.WriteLine($"{item.Title}, {item.Duration}");
            //}

            // Query Syntax

            //var querySyntaxSeven = (from movie in movies
            //                        where movie.Duration <= 120
            //                        select movie.Title).TakeLast(3).ToList();

            //foreach (var item in querySyntaxSeven)
            //{
            //    Console.WriteLine(item);
            //}

            #endregion

            #region 7
            //*Find all movies TITLES and RATING and order them by DURATION(DESC) -no condition needed

            //var moviesSortedByDurationDescending = movies.
            //                         OrderByDescending(movie => movie.Duration).
            //                         Select(movie => new { Title = movie.Title, Rating = movie.Rating , Duration = movie.Duration}).
            //                         ToList();
            //foreach (var item in moviesSortedByDurationDescending)
            //{
            //    Console.WriteLine($"Title: {item.Title}, Movie Rating: {item.Rating}, Movie Duration: {item.Duration}");
            //}
            // Query Syntax

            //var querySyntaxSeven = (from movie in movies
            //                        orderby movie.Duration descending
            //                        select movie).Select(movie => new { movie.Title, movie.Rating, movie.Duration}).ToList();
            //querySyntaxSeven.ForEach(movie => Console.WriteLine(movie));
            #endregion
            #region 8

            //* Find all movies with TITLES that don't start with A and TITLES include more than 7 characters
            //var movieTitlesDontStartWithA = movies.
            //                            Where(movie => !movie.Title.StartsWith("A") && movie.Title.Length > 7);
            //foreach (var item in movieTitlesDontStartWithA)
            //{
            //    Console.WriteLine(item.Title);
            //}

            //Query Syntax
            //var querySyntaxEight = (from movie in movies
            //                        where !movie.Title.StartsWith('A') &&
            //                        movie.Title.Length > 7
            //                        select movie.Title
            //                        ).ToList();
            //foreach (var item in querySyntaxEight)
            //{
            //    Console.WriteLine(item);
            //}
            #endregion

            #region 9
            //* Find all movies RATINGS that have RATINGS higher than 7.2, DURATIONS less then 1hour and 50min

            //var moviesRatingDuration = movies.
            //                           Where(movie => movie.Rating > 7.2 && movie.Duration < 110).
            //                           Select(movie => new { Title = movie.Title , Rating = movie.Rating, Duration = movie.Duration}).
            //                           ToList();
            //foreach (var item in moviesRatingDuration)
            //{
            //    Console.WriteLine($"{item.Title}, {item.Rating}, {item.Duration}");
            //}

            //Query Syntax
            //var querySyntaxNine = (from movie in movies
            //                       where movie.Rating > 7.2 && movie.Duration < 110
            //                       select movie).Select(movie => new { movie.Title, movie.Rating });

            //foreach (var item in querySyntaxNine)
            //{
            //    Console.WriteLine(item);
            //}

            //* Find all movies TITLES and RATINGS that have TITLES with less than 10 characters, DURATION
            //longer than 2 hours, RATING higher then 6.7 and order them by TITLE(ASC)
            #endregion

            #region 10
            //var complexMovieSorting = movies.
            //                         Where(movie => movie.Title.Length < 10 &&
            //                         movie.Duration > 120 &&
            //                         movie.Rating > 6.7).
            //                         Select(movie => movie.Title).
            //                         OrderBy(title => title).
            //                         ToList();

            //foreach (var item in complexMovieSorting)
            //{
            //    Console.WriteLine(item);
            //}

            //var querySyntaxTen = (from movie in movies
            //                      where movie.Title.Length < 10 &&
            //                      movie.Duration > 120 &&
            //                      movie.Rating > 6.7
            //                      select movie).OrderBy(title => title).ToList();

            //querySyntaxTen.ForEach(movie => Console.WriteLine($"{movie.Title} Rating: {movie.Rating}"));
            #endregion
            //Console.ReadLine();
        }
예제 #13
0
        static void Main(string[] args)
        {
            List <Movie> movies = MoviesHelper.GetComedyMovies();


            //Bonus--(SQL Query syntax и Lambda syntax).

            // 1- Find all movies that their titles starts with "L"
            //The basic one like we did it before-
            foreach (var movie in movies)
            {
                if (movie.Title.StartsWith('L'))
                {
                    Console.WriteLine(movie.Title);
                }
            }
            //with Lamda
            var movieFirstLetterL = movies.
                                    Where(movie => movie.Title.StartsWith('L')).ToList();

            foreach (var movie in movieFirstLetterL)
            {
                Console.WriteLine(movie.Title);
            }

            //with Query
            var movieFirstLetterLQuery = (from movie in movies
                                          where movie.Title.StartsWith('L')
                                          select movie.Title).ToList();


            //* 2- Find the NUMBER of movies that have rating higher than 7.5
            //with lamda
            var movieRatingLamda = movies.
                                   Count(movie => movie.Rating > 7.5);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(movieRatingLamda);



            //* 3- Find all movies TITLES with year of production before 2005
            //with lamda
            var movieProductionLamda = movies.
                                       Where(movie => movie.Year <= 2005).ToList();

            Console.ForegroundColor = ConsoleColor.Red;
            foreach (var movie in movieProductionLamda)
            {
                Console.WriteLine(movie.Title);
            }
            //with query
            var movieProductionQuery = (from movie in movies
                                        where movie.Year <= 2005
                                        select movie.Title);

            //* 4-Find all movies TITLES and RATING that have rating higher then 8.0
            //with lamda
            var movieTitleRating = movies
                                   .Where(movie => movie.Rating > 8.0)
                                   .Select(movie => movie.Title)
                                   .ToList();

            Console.ForegroundColor = ConsoleColor.Yellow;
            foreach (var movie in movieTitleRating)
            {
                Console.WriteLine(movie);
            }
            //with query
            var movieTitleRatingQuery = (from movie in movies
                                         where movie.Rating > 8.0
                                         select movie.Title).ToList();


            //*5- Find first 5 movies that have duration time longer then 2 hours
            //lamda
            var movieLongerTwoHoursLamda = movies
                                           .Where(movie => movie.Duration > 120)
                                           .Take(5).ToList();

            Console.ForegroundColor = ConsoleColor.White;
            movieLongerTwoHoursLamda.ForEach(movie => Console.WriteLine(movie.Title));


            //query
            var movieLongerTwoHours = (from movie in movies
                                       where movie.Duration > 120
                                       select movie.Title).Take(5).ToList();

            Console.ForegroundColor = ConsoleColor.Cyan;
            movieLongerTwoHours.ForEach(movie => Console.WriteLine(movie));


            //*6- Find last 3 movies TITLES and DURATION that have duration less then 2 hours
            //lamda
            var movieDruationLess = movies
                                    .Where(movie => movie.Duration < 120)
                                    .Select(movie => movie.Title).TakeLast(3).ToList();

            Console.ForegroundColor = ConsoleColor.DarkYellow;
            movieDruationLess.ForEach(movie => Console.WriteLine(movie));

            //query
            var movieDurationLessQuery = (from movie in movies
                                          where movie.Duration < 120
                                          select movie.Title).TakeLast(3).ToList();

            Console.ForegroundColor = ConsoleColor.Blue;
            movieDurationLessQuery.ForEach(movie => Console.WriteLine(movie));


            //*7- Find all movies TITLES and RATING and order them by DURATION(DESC) -no condition needed
            //lamda
            var RatingTitle = movies
                              .OrderByDescending(duration => duration.Duration)
                              .Select(movie => new { movie.Title, movie.Rating })
                              .ToList();

            Console.ForegroundColor = ConsoleColor.Green;
            RatingTitle.ForEach(movie => Console.WriteLine(movie));

            //query
            var RatingTitleQuery = (from movie in movies
                                    orderby movie.Duration descending
                                    select movie)
                                   .Select(movie => new { movie.Title, movie.Rating }).ToList();

            Console.ForegroundColor = ConsoleColor.Blue;
            RatingTitle.ForEach(movie => Console.WriteLine(movie));



            // 8-*Find all movies with TITLES that don't start with A and TITLES include more than 7 characters
            //lamda
            var titleA = movies
                         .Where(movie => movie.Title.Length > 7 &&
                                !movie.Title.StartsWith('A')).ToList();

            Console.ForegroundColor = ConsoleColor.Red;
            titleA.ForEach(movie => Console.WriteLine(movie.Title));
            //query
            var titleQuery = (from movie in movies
                              where movie.Title.Length > 7 && !movie.Title.StartsWith('A')
                              select movie).ToList();

            //* 9-Find all movies RATINGS that have RATINGS higher than 7.2, DURATIONS less then 1hour
            //  and 50min
            var RatingMoviesHigher = movies
                                     .Where(movie => movie.Rating > 7.2)
                                     .Where(movie => movie.Duration < 110)
                                     .ToList();

            Console.ForegroundColor = ConsoleColor.Yellow;
            RatingMoviesHigher.ForEach(movie => Console.WriteLine(movie.Rating));


            //10- *Find all movies TITLES and RATINGS that have TITLES with less than 10 characters, DURATION
            //  longer than 2 hours, RATING higher then 6.7 and order them by TITLE(ASC)

            //lamda
            var TitleRatingChar = movies
                                  .Where(movie => movie.Title.Length < 10)
                                  .Where(movie => movie.Duration > 120)
                                  .Where(movie => movie.Rating > 6.7)
                                  .Select(movie => movie.Title)
                                  .OrderBy(title => title)
                                  .ToList();

            Console.ForegroundColor = ConsoleColor.Green;
            TitleRatingChar.ForEach(movie => Console.WriteLine(movie));
            //query

            var titleRating = (from movie in movies
                               where movie.Rating > 7.2f && movie.Duration > 110
                               select movie.Rating).ToList();


            Console.ReadLine();
        }