private static void Task2() {// - how many artists end with letter 'a' (case insensitive) var result = Artists.Count(a => a.FullName.ToLower().EndsWith("a")); result.PrintItem(); }
static void Main(string[] args) { Init();// this method fills the arrays above with data /* * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ * ░░░░░░░░░░░░░░░░░░░░░░████████░░░░░░░░░ * ░░███████░░░░░░░░░░███▒▒▒▒▒▒▒▒███░░░░░░ * ░░█▒▒▒▒▒▒█░░░░░░░███▒▒▒▒▒▒▒▒▒▒▒▒███░░░░ * ░░░█▒▒▒▒▒▒█░░░░██▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██░░ * ░░░░█▒▒▒▒▒█░░░██▒▒▒▒▄██▄▒▒▒▒▄██▄▒▒▒███░ * ░░░░░█▒▒▒█░░░█▒▒▒▒▒▒████▒▒▒▒████▒▒▒▒▒██ * ░░░█████████████▒▒▒▒▀██▀▒▒▒▒▀██▀▒▒▒▒▒██ * ░░░█▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒▒▒▒▒▒█▒▒▒▒▒▒▒▒▒▒██ * ░██▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒██▒▒▒▒▒▒▒▒▒██▒▒▒▒██ * ██▒▒▒███████████▒▒▒▒▒██▒▒▒▒▒▒▒██▒▒▒▒▒██ * █▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒▒▒███████▒▒▒▒▒▒▒██ * ██▒▒▒▒▒▒▒▒▒▒▒▒▒▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██░ * ░█▒▒▒███████████▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██░░░ * ░██▒▒▒▒▒▒▒▒▒▒▒███▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█░░░░░ * ░░████████████░░░████████████████░░░░░░ * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ * ░░▄█████▄░▄███████▄░▄███████▄░██████▄░░ * ░░██▒▒▒▒█░███▒▒▒███░███▒▒▒███░██▒▒▒██░░ * ░░██▒▒▒▒▒░██▒▒▒▒▒██░██▒▒▒▒▒██░██▒▒▒██░░ * ░░██▒▒▒▀█░███▒▒▒███░███▒▒▒███░██▒▒▒██░░ * ░░▀█████▀░▀███████▀░▀███████▀░██████▀░░ * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ * ░░░░██▒▒▒▒░██▒▒▒██░▄█████░██▒▒▒▒██▀░░░░ * ░░░░██▒▒▒▒░██▒▒▒██░██▀▒▒▒░██▒▒▒██░░░░░░ * ░░░░██▒▒▒▒░██▒▒▒██░██▒▒▒▒░█████▀░░░░░░░ * ░░░░██▒▒▒▒░██▄▒▄██░██▄▒▒▒░██▒▒▒██░░░░░░ * ░░░░▀█████░▀█████▀░▀█████░██▒▒▒▒██▄░░░░ * ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ */ Action line = () => Console.WriteLine(); //1 - how many Songs start with the letter 'a' (case insensitive) //int StartsWithA=Songs.Where(song=>song.Name.StartsWith("a")).Count(); int StartsWithA2 = Songs.Count(song => song.Name.ToLower().StartsWith("a")); Console.WriteLine($"{StartsWithA2} songs start with the letter a"); line(); //2 - how many artists end with letter 'a' (case insensitive) int ArtistEndWithA = Artists.Count(artist => artist.FullName.ToLower().EndsWith("a")); Console.WriteLine($"{ArtistEndWithA} artist end with the letter a"); line(); //3 - whats the name of the song with longest duration //int SongWithLongestDuration=Songs.Max(song=>song.Duration); var SongNameLongestDuration = Songs.Where(songD => songD.Duration == Songs.Max(song => song.Duration)) .Select(nameSong => nameSong.Name) .FirstOrDefault(); // if there are more songs with the same duration as the max duration it will be shown only the name of the first one Console.WriteLine($"The name of the song with longest duration is {SongNameLongestDuration}"); line(); //4 - whats the total Duration of all Songs int totalDuration = Songs.Sum(song => song.Duration); Console.WriteLine($"Total duration of all songs is {totalDuration} s"); line(); //5 - how many albums have Songs longer than 300 seconds int numOfAlbumsLonger300 = Albums.Count(album => album.Songs.Any(song => song.Duration > 300)); Console.WriteLine($"There are {numOfAlbumsLonger300} albums with songs longer than 300s "); line(); //6 - print the names of the artists(separated with "--"), that have more than one album of PopRock genre var ArtistAlbum = Artists.Join(Albums, ar => ar.Id, a => a.ArtistId, (ar, a) => new { Name = ar.FullName, Albums = ar.Albums, Genre = a.Genre }); var Name = ArtistAlbum.Where(x => x.Genre == Genre.PopRock) .GroupBy(n => n.Name) .Select(a => new { Name = a.Key, Num = a.Count() }) .Where(x => x.Num > 1) .Select(n => n.Name); Console.WriteLine(string.Join("--", Name)); line(); //7 - print the name of the album that has highest Average duration of a song var AverageDuration = Albums.Select(s => new { albumName = s.Name, averageDuration = s.Songs.Average(d => d.Duration) }); var maxADuration = AverageDuration.Max(s => s.averageDuration); var NameAlbumHighestAverageDuraion = AverageDuration .Where(album => album.averageDuration == maxADuration) .Select(s => s.albumName); Console.WriteLine(string.Join(" ", NameAlbumHighestAverageDuraion) + " album with highest average duration of a songs"); line(); //8 - how many characters has the song that has the shortest Duration var shortestDuration = Songs.Min(song => song.Duration); var NameOfThesongWithShortestDuration = Songs.Where(song => song.Duration == shortestDuration) .Select(s => s.Name) .FirstOrDefault(); var Characters = NameOfThesongWithShortestDuration.Replace(" ", "").ToList().Count(); //Console.WriteLine(characters); Console.WriteLine($"The song that has the shortest Duration has {Characters} characters "); line(); //9 - print the name and the genre of the album that has most songs var SongNum = Albums.Select(album => new { name = album.Name, genre = album.Genre, songs = album.Songs.Count() }); var max = SongNum.Max(album => album.songs); var nameAndGenre = SongNum.Where(album => album.songs == max) .Select(n => new { AlbumName = n.name, AlbumGenre = n.genre }); Console.WriteLine(string.Join(" ", nameAndGenre) + " has most songs"); line(); //10 - print the name of the artist that has most songs var ArtistAndNumSong = Artists.Join(Albums, ar => ar.Id, a => a.ArtistId, (ar, a) => new { Name = ar.FullName, numSongs = a.Songs.Count() }); var ArtistSong = ArtistAndNumSong .GroupBy(n => n.Name) .Select(s => new { nameArtis = s.Key, num = s.Sum(k => k.numSongs) }); var maxSong = ArtistSong.Max(song => song.num); var ArtistWithMaxSongs = ArtistSong .Where(s => s.num == maxSong) .Select(n => n.nameArtis); Console.WriteLine("The name of the artist that has the most songs is" + string.Join(" ", ArtistWithMaxSongs)); line(); //11 - print the type of the artist(SoloArtist/Band) that has most albums published before year 2000 //all artists with number of their albums published before 2000 var albumsAndYears = Albums.Where(album => album.Year < 2000) .GroupBy(a => a.ArtistId) .Select(artist => new{ artistId = artist.Key, albumbs = artist.Count() }); //one album and artist id whit max albums published before 2000 var m = albumsAndYears.Max(a => a.albumbs); var id = albumsAndYears.Where(a => a.albumbs == m); //type of the artist var artistType = Artists.Join(id, a => a.Id, i => i.artistId, (a, i) => new { type = a.ArtistType }); Console.WriteLine("The type of the artist that has most albums published before 2000 y is " + string.Join(" ", artistType)); line(); //12 - print the average song duration, of the album that has most songs var SongAndAlbum = Albums.Select(album => new { name = album.Name, averageD = album.Songs.Average(s => s.Duration), songs = album.Songs.Count() }); var maxSongs = SongAndAlbum.Max(album => album.songs); var averageSongDurationMostSongs = SongAndAlbum.Where(album => album.songs == maxSongs) .Select(songs => songs.averageD) .FirstOrDefault(); Console.WriteLine($"The average song duration of the album that has most songs is {averageSongDurationMostSongs}"); line(); // Bonus: //13 - print the longest song duration of the album that has least songs var albumLeast = Albums.Select(s => new { name = s.Name, songs = s.Songs.Count(), id = s.Id }); var minSongInAlbum = albumLeast.Min(s => s.songs); var albumWithLeast = albumLeast.Where(s => s.songs == minSongInAlbum); // min broj na pesni vo album e 8 no ima 2 albumi so po 8 pesni // kako rezultat najdolga pesna vo odnos na dvata albumi so najmal broj pesni var songFromAlbum = albumWithLeast.Join(Songs, a => a.id, s => s.AlbumId, (a, s) => new{ songName = s.Name, duration = s.Duration } ); var songDuration = songFromAlbum.Max(s => s.duration); var LongestSongFromAlbumWithLeastSongs = songFromAlbum.Where(s => s.duration == songDuration) .Select(s => s.songName); Console.WriteLine("The longest song of the album with least songs is " + string.Join(" ", LongestSongFromAlbumWithLeastSongs)); line(); //14 - print the name of the album that has most songs that contain letter 'a' in the name var songsAndLetterA = Songs.Select(s => new { SongName = s.Name, albumId = s.AlbumId }); var numOfA = songsAndLetterA.Select(s => new { vk = s.SongName.ToLower().ToCharArray().Count(a => a.Equals('a')), albumId = s.albumId }) .Where(A => A.vk > 0) .GroupBy(a => a.albumId) .Select(S => new{ idA = S.Key, songs = S.Count() }); var mostWithA = numOfA.Max(a => a.songs); var albumName = numOfA.Where(a => a.songs == mostWithA).Join(Albums, a => a.idA, al => al.Id, (a, al) => new{ nameAlbum = al.Name }); // .Select(s=>new{albumId=s.Key,songsD=s.Count(a=>a.vk)}); // Console.WriteLine(string.Join(" ",numOfA)); Console.WriteLine("The name of the album that has most songs that contain letter a is " + string.Join(" ", albumName)); line(); //15 - print the name of the artist that has most songs that end with letter 'd' //artist id and number of songs that the artist has which ends with d var songsLetterD = Songs.Where(song => song.Name.ToLower().EndsWith("d")) .GroupBy(s => s.AlbumId) .Select(a => new { albumId = a.Key, songsD = a.Count() }); var MaxsongD = songsLetterD.Max(s => s.songsD); var albumId = songsLetterD.Where(s => s.songsD == MaxsongD).Join(Albums, aId => aId.albumId, a => a.Id, (aId, a) => new{ artistId = a.ArtistId } ).Join(Artists, art => art.artistId, a => a.Id, (art, a) => new{ artistName = a.FullName } ); Console.WriteLine("The name of the artist that has most songs that end with letter d is " + string.Join(" ", albumId)); Console.ReadLine(); }