static void QueryingCustomTypes() { List <MusicalArtist> artistsDataSource = Program5.GetMusicalArtists(); IEnumerable <MusicalArtist> artistsResult = from artist in artistsDataSource select artist; Console.WriteLine("\nQuerying Custom Types"); Console.WriteLine("---------------------\n"); foreach (MusicalArtist artist in artistsResult) { Console.WriteLine( "Name: {0}\nGenre: {1}\nLatest Hit: {2}\n", artist.Name, artist.Genre, artist.LatestHit); } }
static void CreatingCustomProjectionsWithTheDataSourceType() { List <MusicalArtist> artistsDataSource = Program5.GetMusicalArtists(); IEnumerable <MusicalArtist> artistsResult = from artist in artistsDataSource select new MusicalArtist { Name = artist.Name, LatestHit = artist.LatestHit }; Console.WriteLine("\nCustom Projection With Data Source Type"); Console.WriteLine("---------------------------------------\n"); foreach (MusicalArtist artist in artistsResult) { Console.WriteLine( "Name: {0}\nLatest Hit: {1}\n", artist.Name, artist.LatestHit); } }
private static void ProjectingAnonymousTypes() { List <MusicalArtist> artistsDataSource = Program5.GetMusicalArtists(); var artistsResult = from artist in artistsDataSource select new { Name = artist.Name, NumberOfAlbums = artist.Albums.Count }; Console.WriteLine("\nProjecting Anonymous Types"); Console.WriteLine("--------------------------\n"); foreach (var artist in artistsResult) { Console.WriteLine( "Artist Name: {0}\nNumber of Albums: {1}\n", artist.Name, artist.NumberOfAlbums); } }
static void CreatingCustomProjectionsOnDifferentType() { List <MusicalArtist> artistsDataSource = Program5.GetMusicalArtists(); IEnumerable <ArtistViewModel> artistsResult = from artist in artistsDataSource select new ArtistViewModel { ArtistName = artist.Name, Song = artist.LatestHit }; Console.WriteLine("\nCustom Projection On a Different Type"); Console.WriteLine("-------------------------------------\n"); foreach (ArtistViewModel artist in artistsResult) { Console.WriteLine( "Artist Name: {0}\nSong: {1}\n", artist.ArtistName, artist.Song); } }