static void Main(string[] args) { using (var ctx = new VidzyContext()) { //var genres = ctx.Genres.ToList(); // //1 Add a new video called "Terminator 1" with action genre, release date Oct 26 198 with silver classification // ctx.Videos.Add(new Video() // { // Classification = Classification.Silver, // Genre = genres.Find(p => p.Name == "Action"), // Name = "Terminator 1", // ReleaseDate = new DateTime(1984, 10, 26) // }); //2 Add two tags "Classics" and "Drama" to the database. // ctx.Tags.AddRange(new List<Tag>() // { // new Tag // { // Name = "Classics" // }, // new Tag() // { // Name = "Drama" // } // }); //add three tags classics, drama, comdedy to the video with id 1. // var vid = ctx.Videos.Single(v => v.Id == 1); // var tags = ctx.Tags.ToList(); // vid.Tags.Add(tags.Find(t => t.Id == 1)); // vid.Tags.Add(tags.Find(t => t.Id == 2)); // vid.Tags.Add(tags.Find(t => t.Id == 3)); // ctx.SaveChanges(); //Remove the comedy tag from the video id=1 // var tags = ctx.Tags.ToList(); // var vid = ctx.Videos.Find(1); // vid.Tags.Remove(tags.Find(t =>t.Id == 3)); // ctx.SaveChanges(); } }
static void Main(string[] args) { //Write a query to load all the videos and display their genres using (var ctx = new VidzyContext()) { //The N+1 Problem var vids = ctx.Videos.ToList(); foreach (var v in vids) { Console.WriteLine(v.Name + " -- " + v.Genre.Name); } Console.WriteLine(); }; //Eager Loading using (var ctx = new VidzyContext()) { var vids = ctx.Videos.Include(v => v.Genre); foreach (var v in vids) { Console.WriteLine(v.Name + " -- " + v.Genre.Name); } Console.WriteLine(); } //Explicit Loading using (var ctx = new VidzyContext()) { var vids = ctx.Videos.ToList(); var vidIds = vids.Select(v => v.Id); ctx.Genres.Where(g => vidIds.Contains(g.Id)).Load(); foreach (var v in vids) { Console.WriteLine(v.Name + " -- " + v.Genre.Name); } } Console.Read(); }
public static void RemoveGenre(int genreId, bool removevides) { using (var context = new VidzyContext()) { var genre = context.Genres.Include(g => g.Videos).SingleOrDefault(g => g.Id == genreId); if (genre == null) { return; } if (removevides) { context.Videos.RemoveRange(genre.Videos); } context.Genres.Remove(genre); context.SaveChanges(); } }
private static void AddVideo(string name, string genreName, DateTime releaseDate, Classification classification) { using (var context = new VidzyContext()) { var genre = context.Genres .Where(g => g.Name == genreName) .First(); var video = new Video() { Name = name, Genre = genre, ReleaseDate = releaseDate, Classification = classification }; context.Videos.Add(video); context.SaveChanges(); } }
static void Main(string[] args) { // To enable lazy loading, you need to declare navigation properties // as virtual. Look at the Video class. var context = new VidzyContext(); var videos = context.Videos.ToList(); Console.WriteLine(); Console.WriteLine("LAZY LOADING"); foreach (var v in videos) { Console.WriteLine("{0} ({1})", v.Name, v.Genre.Name); } // Eager loading var videosWithGenres = context.Videos.Include(v => v.Genre).ToList(); Console.WriteLine(); Console.WriteLine("EAGER LOADING"); foreach (var v in videosWithGenres) { Console.WriteLine("{0} ({1})", v.Name, v.Genre.Name); } // Explicit loading // NOTE: At this point, genres are already loaded into the context, // so the following line is not going to make a difference. If you // want to see expicit loading in action, comment out the eager loading // part as well as the foreach block in the lazy loading. context.Genres.Load(); Console.WriteLine(); Console.WriteLine("EXPLICIT LOADING"); foreach (var v in videos) { Console.WriteLine("{0} ({1})", v.Name, v.Genre.Name); } }
private static void RemoveTagFromVideo(string videoName, string tagName) { using (var context = new VidzyContext()) { var tag = context.Tags .Where(t => t.Name == tagName) .FirstOrDefault(); if (tag != null) { var video = context.Videos .Where(v => v.Name == videoName) .FirstOrDefault(); if (video != null) { video.Tags.Remove(tag); context.SaveChanges(); } } } }
static void Main(string[] args) { var dbContetxt = new VidzyContext(); //Video video = new Video //{ // Name = "Die Hard 3", // Classification = Classification.Silver, // Genre = dbContetxt.Genres.FirstOrDefault(g => g.Name == "Action"), // ReleaseDate = DateTime.Now //}; //dbContetxt.Videos.Add(video); //dbContetxt.SaveChanges(); foreach (var v in dbContetxt.Videos) { Console.WriteLine($"{v.Name} {v.Genre.Name} {v.Classification}"); } Console.ReadLine(); }
public static void AddTagsToVideo(int videoId, params string[] tagNames) { using (var context = new VidzyContext()) { var tags = context.Tags.Where(t => tagNames.Contains(t.Name)).ToList(); foreach (var tagName in tagNames) { if (!tags.Any(t => t.Name.Equals(tagName, StringComparison.CurrentCultureIgnoreCase))) { tags.Add(new Tag { Name = tagName }); } } var video = context.Videos.Single(v => v.Id == videoId); tags.ForEach(t => video.AddTag(t)); context.SaveChanges(); } }
static void Main(string[] args) { var context = new VidzyContext(); System.Console.WriteLine("Exercise 1: Action movies sorted by name"); var query1 = context.Videos.Where(v => v.Genre.Name.Contains("Action")).OrderBy(v => v.Name); foreach (var video in query1) { System.Console.WriteLine("\t" + video.Name); } System.Console.WriteLine("\nExercise 2: Gold drama movies sorted by release date (newest first)"); var query2 = context.Videos .Where(v => v.Classification == Classification.Gold && v.Genre.Name.Contains("Drama")) .OrderBy(v => v.ReleaseDate); foreach (var video in query2) { System.Console.WriteLine("\t" + video.Name); } System.Console.WriteLine("\nExercise 3: All movies projected into an anonymous type with two properties: MovieName and Genre"); var query3 = context.Videos.Select(v => new { MovieName = v.Name, Genre = v.Genre.Name }); foreach (var video in query3) { System.Console.WriteLine("\t" + video.MovieName); } System.Console.ReadKey(); }
static void Main(string[] args) { var context = new VidzyContext(); context.AddVideo("Video 4", DateTime.Today, "Horror", (byte)Classification.Gold); }
static void Main(string[] args) { var context = new VidzyContext(); var videos = context.Videos .Where(c => c.Genre.Name.Equals("Action")) .OrderBy(c => c.Name) .Select(c => new { VideoName = c.Name }); foreach (var c in videos) { Console.WriteLine("{0}", c.VideoName); } Console.WriteLine("-------------------------------------------"); var videos2 = context.Videos .Where(c => c.Genre.Name.Equals("Drama") && c.Classification == Classification.Gold) .OrderByDescending(c => c.ReleaseDate) .Select(c => new { VideoName = c.Name }); foreach (var c in videos2) { Console.WriteLine("{0}", c.VideoName); } Console.WriteLine("-------------------------------------------"); var videos3 = context.Videos .Select(c => new { MovieName = c.Name, Genre = c.Genre.Name }); foreach (var c in videos3) { Console.WriteLine("{0} - {1}", c.MovieName, c.Genre); } Console.WriteLine("-------------------------------------------"); var videos4 = context.Videos .GroupBy(v => v.Classification) .Select(g => new { Classification = g.Key.ToString(), Videos = g.OrderBy(v => v.Name) }); foreach (var g in videos4) { Console.WriteLine("Classification: " + g.Classification); foreach (var v in g.Videos) { Console.WriteLine("\t" + v.Name); } } Console.WriteLine("-------------------------------------------"); var videos5 = context.Videos .GroupBy(v => v.Classification) .Select(g => new { Name = g.Key.ToString(), VideosCount = g.Count() }) .OrderBy(c => c.Name); foreach (var c in videos5) { Console.WriteLine("{0} ({1})", c.Name, c.VideosCount); } Console.WriteLine("-------------------------------------------"); var videos6 = context.Genres .GroupJoin(context.Videos, g => g.Id, v => v.GenreId, (genre, videos1) => new { Name = genre.Name, VideosCount = videos1.Count() }) .OrderByDescending(g => g.VideosCount); foreach (var g in videos6) { Console.WriteLine("{0} ({1})", g.Name, g.VideosCount); } }
static void Main(string[] args) { var context = new VidzyContext(); // Action Movies sorted by name var actionVideos = context.Videos .Where(v => v.Genre.Name == "Action") .OrderBy(v => v.Name) .Select(v => v.Name); foreach (var video in actionVideos) { Console.WriteLine(video); } Console.WriteLine(); //Gold Drama Movies sorted by Releasedate var goldMovies = context.Videos .Where(v => v.Classification == Classification.Gold) .Where(v => v.Genre.Name == "Drama") .OrderBy(v => v.ReleaseDate) .Select(v => v.Name); foreach (var video in goldMovies) { Console.WriteLine(video); } Console.WriteLine(); //All Movies Genre and Name in anonymous type var allMovies = context.Videos .Select(v => new { NameIs = v.Name, TheGenre = v.Genre.Name }); foreach (var allMovie in allMovies) { Console.WriteLine(allMovie); } Console.WriteLine(); //All movies grouped by classification var groupedMovies = context.Videos .GroupBy(v => v.Classification) .Select(g => new { Classification = g.Key.ToString(), Videos = g.OrderBy(v => v.Classification) }); foreach (var groupedMovie in groupedMovies) { Console.WriteLine("Classification: {0}", groupedMovie.Classification); foreach (var video in groupedMovie.Videos) { Console.WriteLine("\t" + video.Name); } } Console.WriteLine(); // List of classifications sorted alpha and count of videos var classifications = context.Videos .GroupBy(v => v.Classification) .Select(g => new { Name = g.Key.ToString(), Count = g.Count() }) .OrderBy(g => g.Name); foreach (var classification in classifications) { Console.WriteLine("{0} ({1})", classification.Name, classification.Count); } Console.WriteLine(); // List of genres and number of videos they include, sorted by the number of videos var genresAndNumberVideos = context.Genres .GroupJoin(context.Videos, g => g.Id, v => v.GenreId, (genre, videos) => new { Name = genre.Name, Count = videos.Count() }) .OrderByDescending(g => g.Count); foreach (var numberVideo in genresAndNumberVideos) { Console.WriteLine("{0} ({1})", numberVideo.Name, numberVideo.Count); } }
static void Main(string[] args) { var context = new VidzyContext(); // Action movies sorted by name Console.WriteLine(); Console.WriteLine("ACTION MOVIES SORTED BY NAME"); var actionMovies = context.Videos .Where(v => v.Genre.Name == "Action") .OrderBy(v => v.Name); foreach (var v in actionMovies) { Console.WriteLine(v.Name); } // Gold drama movies sorted by release date (newest first) var dramaMovies = context.Videos .Where(v => v.Genre.Name == "Drama" && v.Classification == Classification.Gold) .OrderByDescending(v => v.ReleaseDate); Console.WriteLine(); Console.WriteLine("GOLD DRAMA MOVIES SORTED BY RELEASE DATE (NEWEST FIRST)"); foreach (var v in dramaMovies) { Console.WriteLine(v.Name); } // All movies projected into an anonymous type var projected = context.Videos .Select(v => new { MovieName = v.Name, Genre = v.Genre.Name }); Console.WriteLine(); Console.WriteLine("ALL MOVIES PROJECTED INTO AN ANONYMOUS TYPE"); foreach (var v in projected) { Console.WriteLine(v.MovieName); } // All movies grouped by classification var groups = context.Videos .GroupBy(v => v.Classification) .Select(g => new { Classification = g.Key.ToString(), Videos = g.OrderBy(v => v.Name) }); Console.WriteLine(); Console.WriteLine("ALL MOVIES GROUPED BY CLASSIFICATION"); foreach (var g in groups) { Console.WriteLine("Classification: " + g.Classification); foreach (var v in g.Videos) { Console.WriteLine("\t" + v.Name); } } // Classifications and number of videos in them var classifications = context.Videos .GroupBy(v => v.Classification) .Select(g => new { Name = g.Key.ToString(), VideosCount = g.Count() }) .OrderBy(c => c.Name); Console.WriteLine(); Console.WriteLine("CLASSIFICATIONS AND NUMBER OF VIDEOS IN THEM"); foreach (var c in classifications) { Console.WriteLine("{0} ({1})", c.Name, c.VideosCount); } // Genres and number of videos in them var genres = context.Genres .GroupJoin(context.Videos, g => g.Id, v => v.GenreId, (genre, videos) => new { Name = genre.Name, VideosCount = videos.Count() }) .OrderByDescending(g => g.VideosCount); Console.WriteLine(); Console.WriteLine("GENRES AND NUMBER OF VIDEOS IN THEM"); foreach (var g in genres) { Console.WriteLine("{0} ({1})", g.Name, g.VideosCount); } }
static void Main(string[] args) { var context = new VidzyContext(); #region Exercise 1 //var actionMoviesSortedByName = // context.Videos.Where(v => v.Genre.Name == "Action").OrderBy(v => v.Name); //foreach (var actionMovie in actionMoviesSortedByName) //{ // Console.WriteLine(actionMovie.Name); //} #endregion #region Exercise 2 //var goldDramaMoviesSortedByReleaseDate = // context.Videos // .Where(v => v.Classification == Classification.Gold && v.Genre.Name == "Drama") // .OrderByDescending(v => v.ReleaseDate); //foreach(var movie in goldDramaMoviesSortedByReleaseDate) // Console.WriteLine(movie.Name); #endregion #region Exercise 3 //var moviesProjectedIntoAnonymousType = // context.Videos.Select(v => new // { // MovieName = v.Name, // Genre = v.Genre // }); //foreach(var movie in moviesProjectedIntoAnonymousType) // Console.WriteLine("Movie: {0}, Genre: {1}", movie.MovieName, movie.Genre.Name); #endregion #region Exercise 4 //Repasar esto!!! //var groupsOfMoviesGroupedByClassification = // context.Videos.GroupBy(v => v.Classification).Select(g => new // { // Classification = g.Key.ToString(), // Videos = g.OrderBy(v => v.Name) // }); //foreach (var group in groupsOfMoviesGroupedByClassification) //{ // Console.WriteLine("Classification: " + group.Classification); // foreach(var video in group.Videos) // Console.WriteLine("\t" + video.Name); //} #endregion #region Exercise5 //Repasar tambien //var classificationsOrderedByNameWithNumberOfMovies = // context.Videos.GroupBy(v => v.Classification).Select(g => new // { // Name = g.Key.ToString(), // VideosCount = g.Count() // }) // .OrderBy(c=>c.Name); //foreach(var classification in classificationsOrderedByNameWithNumberOfMovies) // Console.WriteLine("{0} ({1})", classification.Name, classification.VideosCount); #endregion #region Exercise 6 //Repasar //var genresWithNumberOfVideosTheyIncludeSortedByNumberOfVideos = // context.Genres // .GroupJoin(context.Videos, g => g.Id, v => v.GenreId, (genre, videos) => new // { // Name = genre.Name, // VideosCount = videos.Count() // }) // .OrderByDescending(a => a.VideosCount); //foreach(var genre in genresWithNumberOfVideosTheyIncludeSortedByNumberOfVideos) // Console.WriteLine("{0} ({1})", genre.Name, genre.VideosCount); #endregion }
static void Main(string[] args) { var context = new VidzyContext(); #region Exercise - 1 // sort action videos sorted by name var actionMovies = context.Videos .Where(v => v.Genre.Name == "Action") .OrderBy(v => v.Name); foreach (var video in actionMovies) { System.Console.WriteLine(video.Name); } System.Console.WriteLine("-----------------------------------"); #endregion #region Exercise - 2 var GoldDramaByDate = context.Videos .Where(v => v.Genre.Name == "Drama" && v.Classification == Classification.Gold) .OrderByDescending(v => v.ReleaseDate); foreach (var drama in GoldDramaByDate) { System.Console.WriteLine(drama.Name); } System.Console.WriteLine("_________________________________________-"); #endregion #region Exercise - 3 var allMoviesProjected = context.Videos .Select(v => new { MovieName = v.Name, GenreMovie = v.Genre.Name }); foreach (var movie in allMoviesProjected) { System.Console.WriteLine("{0} : {1}", movie.MovieName, movie.GenreMovie); } #endregion #region Exercise - 4 var moviesGroupedByClassification = context.Videos .GroupBy(v => v.Classification) .Select(g => new // g has video with key { Classification = g.Key.ToString(), Videos = g.OrderBy(v => v.Name) // videos ordered by }); Console.WriteLine("----------------------------------"); Console.WriteLine("ALL MOVIES GROUPED BY CLASSIFICATION"); foreach (var g in moviesGroupedByClassification) { Console.WriteLine("Classification: " + g.Classification); foreach (var v in g.Videos) { Console.WriteLine("\t" + v.Name); } } #endregion // Exercise - 5 var groups = context.Videos .GroupBy(v => v.Classification) .Select(g => new { Classification = g.Key.ToString(), Count = g.Count() }); foreach (var group in groups) { Console.WriteLine(group.Classification + " : " + group.Count); } // Exercise - 6 var SortedByGenreOrderedByHighest = context.Videos .GroupBy(v => v.Genre.Name) .OrderBy(g => g.Count()) .Select(g => new { Classification = g.Key.ToString(), Count = g.Count() }); foreach (var group in SortedByGenreOrderedByHighest) { Console.WriteLine("{0}({1})", group.Classification, group.Count); } }
static void Main(string[] args) { var context = new VidzyContext(); //Excercise 6 var actionMoviesSortedByName = context.Videos .Where(v => v.Genre.Name == "Action") .OrderBy(v => v.Name); Console.WriteLine("Action movies sorted by name: "); foreach (var movie in actionMoviesSortedByName) { Console.WriteLine("{0}", movie.Name); } Console.WriteLine(""); var goldDramaMoviesSortedByReleaseDate = context.Videos .Where(v => v.Classification == Classification.Gold && v.Genre.Name == "Drama") .OrderByDescending(v => v.ReleaseDate); Console.WriteLine("Gold drama movies sorted by release date (newest first): "); foreach (var movie in goldDramaMoviesSortedByReleaseDate) { Console.WriteLine("{0}", movie.Name); } Console.WriteLine(""); var allMovies = context.Videos.Select(v => new { MovieName = v.Name, Genre = v.Genre.Name }); Console.WriteLine("All movies projected into an anonymous type with two properties - MovieName and Genre: "); foreach (var movie in allMovies) { Console.WriteLine("{0}", movie.MovieName); } Console.WriteLine(""); var allMoviesGroupedByClassification = context.Videos .GroupBy(v => v.Classification) .Select(v => new { Classification = v.Key, Movies = v }) .ToList(); Console.WriteLine("All movies grouped by their classification: "); foreach (var classification in allMoviesGroupedByClassification) { Console.WriteLine("Classification: {0}", classification.Classification); foreach (var movie in classification.Movies) { Console.WriteLine(" {0}", movie.Name); } Console.WriteLine(""); } Console.WriteLine(""); var classificationsWithCounts = context.Videos .GroupBy(v => v.Classification) .Select(v => new { Classification = v.Key, NumberOfVideos = v.Count() }) .OrderBy(c => c.Classification) .ToList(); Console.WriteLine("List of classifications sorted alphabetically and count of videos in them: "); foreach (var classification in classificationsWithCounts) { Console.WriteLine("{0} ({1})", classification.Classification, classification.NumberOfVideos); } Console.WriteLine(""); var genresWithVideoCounts = context.Genres .Select(g => new { GenreName = g.Name, NumberOfVideos = g.Videos.Count() }) .OrderByDescending(c => c.NumberOfVideos) .ToList(); Console.WriteLine("List of genres and number of videos they include, sorted by the number of videos: "); foreach (var genre in genresWithVideoCounts) { Console.WriteLine("{0} ({1})", genre.GenreName, genre.NumberOfVideos); } // Excercice 7 Console.WriteLine("Excercise 7: Loading Related Object"); var allVideosInDb = context.Videos.Include(v => v.Genre).ToList(); Console.WriteLine("All videos in database with Genre: "); foreach (var video in allVideosInDb) { Console.WriteLine("{0} ({1})", video.Name, video.Genre.Name); } }
static void Main(string[] args) { var context = new VidzyContext(); //action movies sorted by name var actionMovies = context.Videos .Where(m => m.Genre.Name == "Action") .OrderBy(m => m.Name); foreach (var actionMov in actionMovies) { //Console.WriteLine(actionMov.Name); } //Gold drama movies sorted by release date var goldDramaMovs = context.Videos .Where(m => m.Classification == Classification.Gold && m.Genre.Name == "Drama") .OrderByDescending(m => m.ReleaseDate); foreach (var goldMov in goldDramaMovs) { //Console.WriteLine(goldMov.Name + " --- " + goldMov.ReleaseDate); } //all movies projected into anonymous type with two props movieName and Genre var movieWithGenre = context.Videos .Select(m => new { movieName = m.Name, genre = m.Genre.Name }); foreach (var mov in movieWithGenre) { //Console.WriteLine(mov.movieName); } //all movies grouped by thier classfication var movsWithClassfication = context.Videos .GroupBy(m => m.Classification) .Select(g => new { classification = g.Key.ToString(), videos = g.OrderBy(v => v.Name) }); foreach (var cla in movsWithClassfication) { //Console.WriteLine("classifictaion : {0} ",cla.classification); foreach (var vid in cla.videos) { //Console.WriteLine(vid.Name); } } //list of classification sorted and count of movies in them var classi = context.Videos .GroupBy(m => m.Classification); foreach (var cla in classi) { Console.WriteLine("{0} ({1})", cla.Key, cla.Count()); } //list of genres and number of videos they include var genres = context.Genres .GroupJoin(context.Videos, g => g.Id, v => v.GenreId, (genre, videos) => new { gen = genre.Name, vids = videos.Count() }); foreach (var vid in genres) { //Console.WriteLine("{0} ({1})",vid.gen,vid.vids); } }
static void Main(string[] args) { var context = new VidzyContext(); var queryActionMovies = context.Videos.Where(m => m.Genre.Name == "Action"); foreach (var movies in queryActionMovies) { System.Console.WriteLine(movies.Name); } var queryGoldDramaMovies = context.Videos .Where(m => m.Genre.Name == "Drama" && m.Classification == Classification.Gold) .OrderBy(m => m.ReleaseDate); foreach (var movie in queryGoldDramaMovies) { System.Console.WriteLine(movie.Name); } var queryAnonymousObj = context.Videos.Select(v => new { MovieName = v.Name, Genre = v.Genre.Name }); foreach (var movie in queryAnonymousObj) { System.Console.WriteLine($"{movie.MovieName}\t {movie.Genre}"); } var queryGroupingByClassification = context.Videos .GroupBy(m => m.Classification) .Select(g => new { Classification = g.Key.ToString(), Movies = g.OrderBy(v => v.Name) }); foreach (var group in queryGroupingByClassification) { System.Console.WriteLine($"Classification: {group.Classification}"); foreach (var movie in group.Movies) { System.Console.WriteLine("\t" + movie.Name); } } var queryGroupAndCounting = context.Videos .GroupBy(m => m.Classification) .Select(g => new { Name = g.Key.ToString(), Count = g.Count() }) .OrderBy(g => g.Name); foreach (var classification in queryGroupAndCounting) { System.Console.WriteLine(classification.Name + classification.Count); } //var query = context.Genres // .Select(g => new // { // Name = g.Name, // VideoCount = g.Videos.Count() // }) // .OrderByDescending(g => g.VideoCount); //foreach (var genre in query) //{ // System.Console.WriteLine(genre.Name + " " + genre.VideoCount); //} }
static void Main(string[] args) { var context = new VidzyContext(); var actionMoviesSortedByName = context.Videos .Where(v => v.Genre.Name == "Action") .OrderBy(v => v.Name); Console.WriteLine("Action movies sorted by name:"); foreach (var movie in actionMoviesSortedByName) { Console.WriteLine(movie.Name); } var goldDramaMoviesSortedByDate = context.Videos .Where(v => v.Classification == Classification.Gold) .Where(v => v.Genre.Name == "Drama") .OrderByDescending(v => v.ReleaseDate); Console.WriteLine(); Console.WriteLine("Gold drama movies sorted by date:"); foreach (var movie in goldDramaMoviesSortedByDate) { Console.WriteLine(movie.Name); } var allMoviesWithGenre = context.Videos .Select(v => new { MovieName = v.Name, Genre = v.Genre.Name }); Console.WriteLine(); Console.WriteLine("All movies with genre:"); foreach (var movie in allMoviesWithGenre) { Console.WriteLine("{0}: {1}", movie.MovieName, movie.Genre); } var allMoviesGroupedByClassification = context.Videos .GroupBy(v => v.Classification) .Select(g => new { Classification = g.Key.ToString(), Movies = g.OrderBy(v => v.Name) }); Console.WriteLine(); Console.WriteLine("All movies grouped by classification"); foreach (var group in allMoviesGroupedByClassification) { Console.WriteLine("Classification: {0}", group.Classification); foreach (var movie in group.Movies) { Console.WriteLine(movie.Name); } } var classificationsWithCount = context.Videos .GroupBy(v => v.Classification) .Select(g => new { Name = g.Key.ToString(), Count = g.Count() }) .OrderBy(g => g.Name); Console.WriteLine(); Console.WriteLine("Classifications with count:"); foreach (var classification in classificationsWithCount) { Console.WriteLine("{0}: {1}", classification.Name, classification.Count); } var genresWithVideosCount = context.Genres .Select(g => new { Name = g.Name, Count = g.Videos.Count() }) .OrderByDescending(g => g.Count); Console.WriteLine(); Console.WriteLine("Genres with video counts:"); foreach (var genre in genresWithVideosCount) { Console.WriteLine("{0}: {1}", genre.Name, genre.Count); } Console.ReadLine(); }
static void Main(string[] args) { var context = new VidzyContext(); // Action movies sorted by name var actionMoviesSorted = context.Videos .Where(v => v.GenreId == 2) .OrderBy(v => v.Name); foreach (var video in actionMoviesSorted) { Console.WriteLine(video.Name); } Console.WriteLine(); // Gold drama movies sorted by release date (newest first) var goldDramaMovies = context.Videos .Where(v => v.GenreId == 7 && v.Classification == Classification.Gold) .OrderByDescending(v => v.ReleaseDate); foreach (var video in goldDramaMovies) { Console.WriteLine(video.Name); } Console.WriteLine(); // All movies projected into an anonymous type with two properties: MovieName and Genre var anonymousMovie = context.Videos .Select(v => new { MovieName = v.Name, Genre = v.Genre.Name }); foreach (var video in anonymousMovie) { Console.WriteLine($@"{video.MovieName}: {video.Genre}"); } Console.WriteLine(); // All movies grouped by their classification var moviesGroupedByClassification = context.Videos .GroupBy(v => v.Classification, (classification, videos) => new { Classification = classification, Movies = videos.OrderBy(v => v.Name) }); foreach (var movieCollection in moviesGroupedByClassification) { Console.WriteLine($@"Classification: {movieCollection.Classification}"); foreach (var video in movieCollection.Movies) { Console.WriteLine($@" {video.Name}"); } } Console.WriteLine(); // List of classifications sorted alphabetically and count of videos in them var classificationsSorted = context.Videos .GroupBy(v => v.Classification, (classification, videos) => new { Classification = classification, Count = videos.Count() }) .OrderBy(g => g.Classification.ToString()); foreach (var group in classificationsSorted) { Console.WriteLine($@"{group.Classification} ({group.Count})"); } Console.WriteLine(); // List of genres and number of videos they include, sorted by the number of videos var genresSorted = context.Genres .GroupJoin(context.Videos, genre => genre.Id, video => video.GenreId, (genre, videos) => new { Genre = genre.Name, Count = videos.Count() }) .OrderByDescending(g => g.Count); foreach (var group in genresSorted) { Console.WriteLine($@"{group.Genre} ({group.Count})"); } }
static void Main(string[] args) { var context = new VidzyContext(); var genres = context.Genres .GroupJoin(context.Videos, g => g.Id, v => v.GenreId, (genre, videos) => new { Genre = genre.Name, Videos = videos.Count() }).OrderByDescending(g => g.Videos); foreach (var g in genres) { Console.WriteLine(g.Genre + " (" + g.Videos.ToString() + ")"); } /*foreach(Classification c in Enum.GetValues(typeof(Classification))) * { * var cnt = context.Videos * .Where(v => v.Classification == c) * .Count(); * * Console.WriteLine(c.ToString() + "(" + cnt + ")"); * }*/ /*var groups = context.Videos * .GroupBy(v => v.Classification) * .Select(g => new { Classification = g.Key, Videos = g }); * * foreach(var c in groups) * { * Console.WriteLine("Classification: " + c.Classification.ToString()); * * foreach(var v in c.Videos) * { * Console.WriteLine("\t" + v.Name); * } * }*/ /*var query = context.Videos * .Where(v => v.Genre.Name == "Action") * .OrderBy(v => v.Name);*/ /*var query = context.Videos * .Where(v => v.Classification == Classification.Gold) * .Where(v => v.Genre.Name == "Drama") * .OrderByDescending(v => v.ReleaseDate);*/ /*var query = context.Videos * .Select(v => new * { * Name = v.Name, * Genre = v.Genre.Name * }); * * foreach(var video in query) * { * Console.WriteLine(video.Name); * }*/ Console.ReadLine(); }
static void Main(string[] args) { var context = new VidzyContext(); var movies = context.Videos; //----- EXERCISE 1 //var query = movies.Where(m => m.Genre.Name == "action").OrderBy(m => m.Name).Select(m => m); //foreach(var m in query) //{ // Console.WriteLine(m.Name); //} //----- EXERCISE 2 //var query = movies.Where(m => m.Genre.Name == "drama" && m.Classification == Classification.Gold).OrderByDescending(m => m.ReleaseDate); //foreach(var m in query) //{ // Console.WriteLine(m.Name); //} //----- EXERCISE 3 //var query = movies.Select(m => new { MovieName = m.Name, Genre = m.Genre.Name }); //foreach(var m in query) //{ // Console.WriteLine(m.MovieName + "\t\t" + m.Genre); //} //----- EXERCISE 4 //var groups = movies // .GroupBy(m => m.Classification) // .Select(g => new // { // Classification = g.Key.ToString(), // Movies = g.OrderBy(v => v.Name) // }); //foreach(var g in groups) //{ // Console.WriteLine(g.Classification); // foreach(var m in g.Movies) // { // Console.WriteLine("\t" + m.Name); // } //} //-----EXERCISE 5 //var groups = movies // .GroupBy(m => m.Classification) // .Select(g => new // { // Classification = g.Key.ToString(), // MovieCount = g.Count() // }) // .OrderBy(g => g.Classification); //foreach(var g in groups) //{ // Console.WriteLine(g.Classification + " - " + g.MovieCount); //} //----- EXERCISE 6 //var query = context.Genres // .Select(g => new // { // Genre = g.Name, // VideoCount = g.Videos.Count() // }) // .OrderByDescending(g => g.VideoCount); //foreach(var g in query) //{ // Console.WriteLine("{0} ({1})", g.Genre, g.VideoCount); //} Console.ReadKey(); }
static void Main(string[] args) { var context = new VidzyContext(); var query1 = context.Videos.Where(c => c.Genre.Name == "Action") .OrderBy(s => s.Name); Console.WriteLine("Action movies sorted by name \n"); foreach (var item in query1) { Console.WriteLine(item.Name); } //=================================================================================================================== var query2 = context.Videos.Where(c => c.Genre.Name == "Drama" && c.Classification == Classification.Gold) .OrderByDescending(s => s.ReleaseDate); Console.WriteLine("\nGold drama movies sorted by release date (newest first) \n"); foreach (var item in query2) { Console.WriteLine(item.Name); } //=================================================================================================================== var query4 = context.Videos.Select(c => new { MovieName = c.Name, Genre = c.Genre.Name }).OrderBy(c => c.MovieName); Console.WriteLine("\nAll movies projected into an anonymous type with two properties:MovieName and Genre\n"); foreach (var item in query4) { Console.WriteLine(item.MovieName); } //==================================================================================================================== var query5 = context.Videos.GroupBy(c => c.Classification) .Select(d => new { Classification = d.Key.ToString(), Video = d.OrderBy(c => c.Name) }); Console.WriteLine("All movies grouped by their classification:"); foreach (var item in query5) { Console.WriteLine($" classification:{item.Classification}"); foreach (var i in item.Video) { Console.WriteLine(i.Name); } } //==================================================================================================================== var query6 = context.Videos.GroupBy(c => c.Classification).Select(x => new { Classification = x.Key.ToString(), count = x.Count() }).OrderBy(p => p.Classification); Console.WriteLine("List of classifications sorted alphabetically and count of videos in them"); foreach (var item in query6) { Console.WriteLine("{0}({1})", item.Classification, item.count); } //===================================================================================================================== var query7 = context.Genres.GroupJoin(context.Videos, o => o.Id, i => i.GenreId, (x, y) => new { name = x.Name, videocount = y.Count() }); foreach (var item in query7) { Console.WriteLine("{0}({1})", item.name, item.videocount); } Console.ReadKey(); }
static void Main(string[] args) { var context = new VidzyContext(); var actionMovies = context.Videos.Where(c => c.GenreId == 2).OrderBy(c => c.Name); foreach (var item in actionMovies) { Console.WriteLine(item.Name); } Console.WriteLine("\n"); var drama = context.Videos .Where(c => c.GenreId == 7 && c.Classification == Classification.Gold) .OrderByDescending(c => c.ReleaseDate); foreach (var item in drama) { Console.WriteLine(item.Name); } Console.WriteLine("\n"); var allmovies = context.Videos.Select(c => new { MovieName = c.Name, GenreName = c.Genre.Name }); foreach (var item in allmovies) { Console.WriteLine(item.MovieName); } Console.WriteLine("\n"); var allmoviegroupbyclass = context.Videos.GroupBy(c => c.Classification); foreach (var classi in allmoviegroupbyclass) { Console.WriteLine("Classification:" + classi.Key); foreach (var movie in classi) { Console.WriteLine(movie.Name); } Console.WriteLine("\n"); } Console.WriteLine("\n"); var listofclassification = context.Videos.GroupBy(c => c.Classification); foreach (var item in listofclassification) { Console.WriteLine("{0} ({1})", item.Key, item.Count()); } Console.WriteLine("\n"); var listofgenres = context.Genres .GroupJoin(context.Videos, g => g.Id, c => c.GenreId, (genre, vides) => new { GenreName = genre.Name, Count = vides.Count() }).OrderByDescending(v => v.Count); foreach (var item in listofgenres) { Console.WriteLine("{0} ({1})", item.GenreName, item.Count); } // To enable lazy loading, you need to declare navigation properties // as virtual. Look at the Video class. var videos = context.Videos.ToList(); Console.WriteLine(); Console.WriteLine("LAZY LOADING"); foreach (var v in videos) { Console.WriteLine("{0} ({1})", v.Name, v.Genre.Name); } // Eager loading var videosWithGenres = context.Videos.Include(v => v.Genre).ToList(); Console.WriteLine(); Console.WriteLine("EAGER LOADING"); foreach (var v in videosWithGenres) { Console.WriteLine("{0} ({1})", v.Name, v.Genre.Name); } // Explicit loading // NOTE: At this point, genres are already loaded into the context, // so the following line is not going to make a difference. If you // want to see expicit loading in action, comment out the eager loading // part as well as the foreach block in the lazy loading. context.Genres.Load(); Console.WriteLine(); Console.WriteLine("EXPLICIT LOADING"); foreach (var v in videos) { Console.WriteLine("{0} ({1})", v.Name, v.Genre.Name); } }
static void Main(string[] args) { using (var ctx = new VidzyContext()) { var x = ctx.Videos.ToList(); foreach (var vid in x) { Console.WriteLine(vid.Name); } } return; using (var ctx = new VidzyContext()) { var genres = ctx.Genres.ToList(); Console.WriteLine("===========Action movies sorted by name"); var actnMoviesByName = ctx.Videos .Where(v => v.GenreId == 2) .OrderBy(v => v.Name); foreach (var video in actnMoviesByName) { Console.WriteLine(video.Name); } Console.WriteLine(); Console.WriteLine("===========Drama movies sorted by release date"); var dramaMoviesByReleaseDateDesc = ctx.Videos .Where(v => v.Classification == Classification.Gold && v.GenreId == 7) .OrderByDescending(v => v.ReleaseDate); foreach (var video in dramaMoviesByReleaseDateDesc) { Console.WriteLine(video.Name); } Console.WriteLine(); Console.WriteLine("========All movies projected into an anonymous type with two properties: MovieName and Genre"); var projection = ctx.Videos.Select( m => new { Name = m.Name, Genre = m.Genre.Name }); foreach (var x1 in projection) { Console.WriteLine(x1.Name + ":" + x1.Genre); } Console.WriteLine(); Console.WriteLine("========All movies grouped by classification"); var query = from v in ctx.Videos group v by v.Classification into g select g; foreach (var g in query.OrderBy(g => g.Key)) { Console.WriteLine("\t" + g.Key.ToString() + ":"); foreach (var video in g.OrderBy(v => v.Name)) { Console.WriteLine("\t" + video.Name); } Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("========List of Classifications sorted alphabetically and their video count"); var query2 = from v in ctx.Videos group v by v.Classification into g select new { Classification = g.Key.ToString(), Count = g.Count() }; foreach (var g in query2.OrderBy(g => g.Classification)) { Console.WriteLine(g.Classification + $"({g.Count})"); } Console.WriteLine(); Console.WriteLine("=======List of Genres and number of videos they include, sorted by video count"); var query3 = from gen in ctx.Genres select new { Genre = gen.Name, Count = gen.Videos.Count() }; foreach (var obj in query3.OrderByDescending(o => o.Count)) { Console.WriteLine(obj.Genre + $"({obj.Count})"); } } }
private static void Main(string[] args) { var dbContext = new VidzyContext(); // ********************************** LINQ Syntax ************************************ System.Console.WriteLine("\n********************************** LINQ Syntax ************************************"); // LINQ Syntax - Action movies sorted by name // LINQ Syntax - Action movies sorted by name // LINQ Syntax - Action movies sorted by name System.Console.WriteLine("\nLINQ Syntax - Action movies sorted by name:"); var videos = from v in dbContext.Videos where v.Genre.Name.Contains("Action") orderby v.Name select v; foreach (var video in videos) { System.Console.WriteLine("\t" + video.Name); } // LINQ Syntax - Gold drama movies sorted by release date (newest first) // LINQ Syntax - Gold drama movies sorted by release date (newest first) // LINQ Syntax - Gold drama movies sorted by release date (newest first) System.Console.WriteLine("\nLINQ Syntax - Gold drama movies sorted by release date (newest first):"); videos = from v in dbContext.Videos where v.Genre.Name.Contains("Drama") && v.Classification == Classification.Gold orderby v.ReleaseDate descending select v; foreach (var video in videos) { System.Console.WriteLine("\t" + video.Name); } // LINQ Syntax - All movies projected into an anonymous type with two properties: MovieName and Genre // LINQ Syntax - All movies projected into an anonymous type with two properties: MovieName and Genre // LINQ Syntax - All movies projected into an anonymous type with two properties: MovieName and Genre System.Console.WriteLine("\nLINQ Syntax - All movies projected into an anonymous type with two properties: MovieName and Genre:"); var videosQ = from v in dbContext.Videos select new { MovieName = v.Name, Genre = v.Genre.Name }; foreach (var video in videosQ) { System.Console.WriteLine("\tName: {0}, Genre: {1}", video.MovieName, video.Genre); } // LINQ Syntax - All movies grouped by their classification // LINQ Syntax - All movies grouped by their classification // LINQ Syntax - All movies grouped by their classification System.Console.WriteLine("\nLINQ Syntax - All movies grouped by their classification:"); var groups = from v in dbContext.Videos orderby v.Name group v by v.Classification into g select new { Classification = g.Key, Videos = g }; foreach (var group in groups) { System.Console.WriteLine("Group Classification(key): " + group.Classification); System.Console.WriteLine("\t" + group.Videos); } // LINQ Syntax - List of classifications sorted alphabetically and count of videos in them. // LINQ Syntax - List of classifications sorted alphabetically and count of videos in them. // LINQ Syntax - List of classifications sorted alphabetically and count of videos in them. System.Console.WriteLine("\nLINQ Syntax - List of classifications sorted alphabetically and count of videos in them:"); var groups2 = from v in dbContext.Videos orderby v.Classification group v by v.Classification into g select new { Classification = g.Key, VideosQuantity = g.Count() }; foreach (var group in groups2) { System.Console.WriteLine("Classification: {0}, Number of Videos: {1}", group.Classification, group.VideosQuantity); } // LINQ Syntax - List of genres and number of videos they include, sorted by the number of videos // LINQ Syntax - List of genres and number of videos they include, sorted by the number of videos // LINQ Syntax - List of genres and number of videos they include, sorted by the number of videos System.Console.WriteLine("\nLINQ Syntax - List of genres and number of videos they include, sorted by the number of videos:"); var groups3 = from gen in dbContext.Genres join vid in dbContext.Videos on gen.Id equals vid.GenreId into g orderby gen.Videos.Count() descending select new { GenreName = gen.Name, VideosQuantity = gen.Videos.Count() }; foreach (var group in groups3) { System.Console.WriteLine("Genre: {0}, Number of Videos: {1}", group.GenreName, group.VideosQuantity); } // ****************************** LINQ Extension Methods ****************************** System.Console.WriteLine("\n****************************** LINQ Extension Methods ******************************"); // LINQ Extension Methods - Action movies sorted by name // LINQ Extension Methods - Action movies sorted by name // LINQ Extension Methods - Action movies sorted by name System.Console.WriteLine("\nLINQ Extension Methods - Action movies sorted by name:"); videos = dbContext.Videos .Where(v => v.Genre.Name == "Action") .OrderBy(v => v.Name); foreach (var video in videos) { System.Console.WriteLine("\t" + video.Name); } // LINQ Extension Methods - Gold drama movies sorted by release date (newest first) // LINQ Extension Methods - Gold drama movies sorted by release date (newest first) // LINQ Extension Methods - Gold drama movies sorted by release date (newest first) System.Console.WriteLine("\nLINQ Extension Methods - Gold drama movies sorted by release date (newest first):"); videos = dbContext.Videos .Where(v => v.Genre.Name == "Drama" && v.Classification == Classification.Gold) .OrderByDescending(v => v.ReleaseDate); foreach (var video in videos) { System.Console.WriteLine("\t" + video.Name); } // LINQ Extension Methods - All movies projected into an anonymous type with two properties: MovieName and Genre // LINQ Extension Methods - All movies projected into an anonymous type with two properties: MovieName and Genre // LINQ Extension Methods - All movies projected into an anonymous type with two properties: MovieName and Genre System.Console.WriteLine("\nLINQ Extension Methods - All movies projected into an anonymous type with two properties: MovieName and Genre:"); var videos2 = dbContext.Videos.Select(v => new { MovieName = v.Name, Genre = v.Genre.Name }); foreach (var video in videos2) { System.Console.WriteLine("\tName: {0}, Genre: {1}", video.MovieName, video.Genre); } // LINQ Extension Methods - All movies grouped by their classification // LINQ Extension Methods - All movies grouped by their classification // LINQ Extension Methods - All movies grouped by their classification System.Console.WriteLine("\nLINQ Extension Methods - All movies grouped by their classification:"); var groups4 = dbContext.Videos.GroupBy(v => v.Classification).Select(g => new { Classification = g.Key.ToString(), Videos = g.OrderBy(v => v.Name) });; foreach (var group in groups4) { System.Console.WriteLine("Group Classification(key): " + group.Classification); foreach (var movie in group.Videos) { System.Console.WriteLine("\t" + movie.Name); } } // LINQ Extension Methods - List of classifications sorted alphabetically and count of videos in them // LINQ Extension Methods - List of classifications sorted alphabetically and count of videos in them // LINQ Extension Methods - List of classifications sorted alphabetically and count of videos in them System.Console.WriteLine("\nLINQ Extension Methods - List of classifications sorted alphabetically and count of videos in them:"); var groups1 = dbContext.Videos .GroupBy(v => v.Classification) .Select(g => new { ClassificationName = g.Key.ToString(), VideosQuantity = g.Count() }).OrderBy(g => g.ClassificationName); foreach (var group in groups1) { System.Console.WriteLine("Classification: {0}, Number of Videos: {1}", group.ClassificationName, group.VideosQuantity); } // LINQ Extension Methods - List of genres and number of videos they include, sorted by the number of videos // LINQ Extension Methods - List of genres and number of videos they include, sorted by the number of videos // LINQ Extension Methods - List of genres and number of videos they include, sorted by the number of videos System.Console.WriteLine("\nLINQ Extension Methods - List of genres and number of videos they include, sorted by the number of videos:"); var groups5 = dbContext.Genres.GroupJoin(dbContext.Videos, g => g.Id, v => v.GenreId, (genre, video) => new { GenreName = genre.Name, VideosQuantity = video.Count() }).OrderByDescending(g => g.VideosQuantity); foreach (var group in groups5) { System.Console.WriteLine("Genre: {0}, Number of Videos: {1}", group.GenreName, group.VideosQuantity); } System.Console.ReadLine(); }