public static void ThirdExercise() { //Take all actors Orderby inverse and GroupBy by sex var sexActors = DataGeneration.CreateMovies().SelectMany(x => x.Actors) .OrderBy(x => x.Name) .GroupBy(x => x.Sex); }
public static void FirstExercise() { //Convert TimeSpan from movies to Minutes duration. var movies = DataGeneration.CreateMovies(); var durationMovies = movies.Select(x => x.Duration); }
public static void SecondExercise() { //Convert Movies and take only the Name of actors var movies = DataGeneration.CreateMovies(); var nameOfActors = movies.SelectMany(x => x.Actors).Select(x => x.Name); }
public static void ThirdExercise() { //Create a new collection with Name of Movie and collection Actors name. var movies = DataGeneration.CreateMovies(); var movieAndActors = movies.Select(x => new Tuple <string, List <Actor> >(x.Name, x.Actors)); }
public static void ForthSample() { //You can take or skip N elements from a collection to take that you want. //It's really useful when create a index. List <Movie> movies = DataGeneration.CreateMovies(); IEnumerable <Movie> moviesSkipped = movies.Skip(2).Take(5); }
public static void FirstSample() { //We can filter and get a new collection with Where operator. List <Movie> movies = DataGeneration.CreateMovies(); //Always need to return a boolean in Lambda function IEnumerable <Movie> actionMovies = movies.Where(x => x.Genre == GenreEnum.Action); foreach (var movie in actionMovies) { Console.WriteLine(movie.Name); } }
public static void ThirdSample() { List <Movie> movies = DataGeneration.CreateMovies(); //We can create a flat collection from another in this case use the SelectMany Linq operator IEnumerable <Actor> actors = movies.SelectMany(x => x.Actors); foreach (var actor in actors) { Console.WriteLine(actor); } }
public static void SecondSample() { List <Movie> movies = DataGeneration.CreateMovies(); //We can use two or more Lambda operators concatenated. IEnumerable <Movie> resultMovies = movies.Where(x => x.Genre == GenreEnum.Action) .Where(x => x.Duration >= TimeSpan.FromMinutes(80)); //You can use the Where operator inside of a collection if there another collection IEnumerable <Movie> moviesWithActorOld15 = resultMovies.Where(x => x.Actors.Any(y => y.Age >= 15)); Console.WriteLine(moviesWithActorOld15.Count()); }
public static void FirstSample() { //You can Orderby the collection for an element and create a orderedCollection List <Movie> movies = DataGeneration.CreateMovies(); IOrderedEnumerable <Movie> moviesOrderedByGenre = movies.OrderBy(x => x.Genre); //Elements it's ordered by Genre foreach (var movieOrdered in moviesOrderedByGenre) { Console.WriteLine(movieOrdered); } }
public static void SecondSample() { List <Movie> movies = DataGeneration.CreateMovies(); //We can create to another class too. IEnumerable <Tuple <string, TimeSpan> > nameAndDurationMovies = movies.Select(x => new Tuple <string, TimeSpan>(x.Name, x.Duration)); //In this case we create in Tuple<string, TimeSpan>> foreach (var elem in nameAndDurationMovies) { Console.WriteLine($"{elem.Item1} {elem.Item2}"); } }
public static void ThirdSample() { //Linq have a First and Last operators that allows take the first Element or the LastOperator from a collection List <Movie> movies = DataGeneration.CreateMovies(); Movie firstMovie = movies.First(); Movie lastMovie = movies.Last(); //You can use Lambda like Where operator Movie firstMovieOfTerror = movies.First(x => x.Genre == GenreEnum.Terror); Movie lastMovieOfTerror = movies.Last(x => x.Genre == GenreEnum.Terror); //There and implementation Operator with OrDefault, that take the Default value. (Null in Class case) Movie firstMovieOfTerrorOrDefault = movies.FirstOrDefault(); Movie lastMoviewOfTerrorOrDefault = movies.LastOrDefault(); }
public static void FirstSample() { //Convert a bunch of data in another type List <Movie> movies = DataGeneration.CreateMovies(); //We have the movies. Take only the Name from Movies IEnumerable <string> moviesName = movies.Select(x => x.Name); //The lambda it's x => x.Name that express a String return foreach (var name in moviesName) { Console.WriteLine(name); } }
public static void SecondSample() { //We can group elements by an element from our collection List <Movie> movies = DataGeneration.CreateMovies(); //The collection have a Key (In this case it's Genre) and a values that it's a collection with every movie order by Genre. IEnumerable <IGrouping <GenreEnum, Movie> > moviesGroupByGenre = movies.GroupBy(x => x.Genre); foreach (var orderElements in moviesGroupByGenre) { //There a Key and Values inside it's collected in a Collection Console.WriteLine(orderElements.Key); Console.WriteLine(orderElements.Count()); } }
public static void FirstExercise() { //Movies Orderby duration var moviesOrdered = DataGeneration.CreateMovies().OrderBy(x => x.Duration); }
public static void FirstExercise() { //Filter actions movies and get the last one var lastActionMovie = DataGeneration.CreateMovies().LastOrDefault(x => x.Genre == GenreEnum.Action); }
public static void SecondExercise() { //Filter comedy movies and Duration it's 90 minutes or more var comedyMoviesMore90 = DataGeneration.CreateMovies().Where(x => x.Genre == GenreEnum.Comedy && x.Duration >= TimeSpan.FromMinutes(90)); }
public static void SecondExercise() { //Take all actors from collection and GroupBy by Age. var actorsByAge = DataGeneration.CreateMovies().SelectMany(x => x.Actors).GroupBy(x => x.Age); }
public static void ThirdExercise() { //Take the first element or Default that have an actor that old 40 or more. var actor40 = DataGeneration.CreateMovies().SelectMany(x => x.Actors).FirstOrDefault(x => x.Age >= 40); }