public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); //========================================================= //Solve all of the prompts below using various LINQ queries //========================================================= //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? //========================================================= //========================================================= //Who is the youngest artist in our collection of artists? //========================================================= //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= //========================================================= //Display all groups with names less than 8 characters //========================================================= //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //========================================================= }
public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); //========================================================= //Solve all of the prompts below using various LINQ queries //========================================================= //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? //========================================================= IEnumerable <Artist> artistQuery = from artist in Artists where artist.Hometown == "Mount Vernon" select artist; foreach (Artist artist in artistQuery) { System.Console.WriteLine(artist.ArtistName + ", " + artist.Age); } //another way to do it Artists.Where(e => e.Hometown == "Mount Vernon").ToList().ForEach(e => System.Console.WriteLine(e.ArtistName + ", " + e.Age)); //========================================================= //Who is the youngest artist in our collection of artists? //========================================================= var min = (from person in Artists select person.Age).Min(); var youngest = from person in Artists where person.Age == min select person; foreach (Artist artist in youngest) { System.Console.WriteLine(artist.ArtistName + ", " + artist.Age); } //another way to do it var min2 = Artists.Min(e => e.Age); Artists.Where(p => p.Age == min2).ToList().ForEach(e => System.Console.WriteLine(e.ArtistName + ", " + e.Age)); //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= var williams = (from people in Artists where people.RealName.Contains("William") select people); foreach (Artist artist in williams) { System.Console.WriteLine(artist.ArtistName + ", Real name: " + artist.RealName + ", " + artist.Age); } //========================================================= //Display all groups with names less than 8 characters //========================================================= var eightChars = (from match in Groups where (match.GroupName).Count() > 8 select match); foreach (Group group in eightChars) { System.Console.WriteLine(group.GroupName); } //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= var oldest = (from artist in Artists orderby artist.Age descending select artist).Take(3); foreach (Artist person in oldest) { System.Console.WriteLine(person.ArtistName + ", " + person.Age); } //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //========================================================= }
public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); //========================================================= //Solve all of the prompts below using various LINQ queries //========================================================= //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? //========================================================= var artist = Artists.Where(q => q.Hometown == "Mount Vernon").ToList(); System.Console.WriteLine(artist[0].ArtistName + " " + artist[0].Age); //========================================================= //Who is the youngest artist in our collection of artists? //========================================================= var artist2 = Artists.OrderBy(str => str.Age).ToList(); System.Console.WriteLine(artist2[0].ArtistName); //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= var artist3 = Artists.Where(q => q.RealName.Contains("William")).ToList(); foreach (var art in artist3) { System.Console.WriteLine(art.RealName); } //========================================================= //Display all groups with names less than 8 characters //========================================================= var group = Groups.Where(q => q.GroupName.Length <= 8); foreach (var member in group) { System.Console.WriteLine(member.GroupName); } //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= var artist4 = Artists.OrderByDescending(q => q.Age).ToList(); System.Console.WriteLine(artist4[0].ArtistName + ", " + artist4[1].ArtistName + "," + artist4[2].ArtistName); //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //========================================================= // var artist6 = Groups.Join(Artists, group2=>group2.Id, artist8=>artist8.GroupId, (group2, artist8) => {group2.Members = artist8; return artist}); // not working }
public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); //========================================================= //Solve all of the prompts below using various LINQ queries //========================================================= //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? //========================================================= var foundArtist = from artist in Artists where artist.Hometown == "Mount Vernon" select new{ artist.RealName, artist.Age }; foreach (var person in foundArtist) { Console.WriteLine(person); } //========================================================= //Who is the youngest artist in our collection of artists? //========================================================= var youngestArtist = Artists.Min(artist => artist.Age + " - " + artist.RealName); Console.WriteLine(youngestArtist); //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= var williamsArtist = from artist in Artists where artist.RealName.Contains("William") select new{ artist.RealName }; foreach (var person in williamsArtist) { Console.WriteLine(person); } //========================================================= //Display all groups with names less than 8 characters //========================================================= var smallGroups = from groups in Groups where groups.GroupName.Length < 8 select new{ groups.GroupName }; foreach (var group in smallGroups) { Console.WriteLine(group); } //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= // var oldArtist = (from artist in Artists orderby artist.Age select artist).Take(3); var oldArtist = Artists.Where(artist => artist.Hometown == "Atlanta").OrderByDescending(artist => artist.Age).Take(3).Select(artist => new{ artist.RealName, artist.Age, artist.Hometown }); foreach (var person in oldArtist) { Console.WriteLine(person); } //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= var groupNotNY = from groups in Groups join artist in Artists on groups.Id equals artist.GroupId where artist.Hometown != "New York City" group groups by groups.GroupName into newGroup select newGroup.Key; foreach (var groupObj in groupNotNY) { Console.WriteLine(groupObj); } //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //=========================================================r var wuTangClan = from artist in Artists join groups in Groups on artist.GroupId equals groups.Id where groups.GroupName == "Wu-Tang Clan" select new{ groups.GroupName, artist.ArtistName }; foreach (var person in wuTangClan) { Console.WriteLine(person); } }
public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); //========================================================= //Solve all of the prompts below using various LINQ queries //========================================================= //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? //========================================================= var foundArtist = from artist in Artists where artist.Hometown == "Mount Vernon" select new{ artist.RealName }; foreach (var person in foundArtist) { // Console.WriteLine(person); } //========================================================= //Who is the youngest artist in our collection of artists? //========================================================= // Solution 1 var test = Artists.OrderBy(artist => artist.Age).First(); // Console.WriteLine(test.ArtistName + "-" + test.Age); // Solution 2 var youngestAge = Artists.Min(artist => artist.Age); var youngestArtist = Artists.Where(artist => artist.Age == youngestAge); // Console.WriteLine(youngestAge); // Console.WriteLine(youngestArtist); foreach (var artist in youngestArtist) { // Console.WriteLine($"The youngest artist is {artist.ArtistName}, who is {artist.Age}."); } //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= var williams = Artists.Where(artist => artist.RealName.Contains("William")); foreach (var instance in williams) { // Console.WriteLine(instance.RealName); } //========================================================= //Display all groups with names less than 8 characters //========================================================= var shorterThan8Char = Groups.Where(group => group.GroupName.Length < 8); foreach (var group in shorterThan8Char) { // Console.WriteLine(group.GroupName); } //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= var oldestFromAtlanta = Artists .Where(artist => artist.Hometown == "Atlanta") .OrderByDescending(artist => artist.Age) .Take(3); foreach (var artist in oldestFromAtlanta) { // Console.WriteLine($"{artist.ArtistName}, Age: {artist.Age}"); } //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= var artistsWithGroup = Artists.Join(Groups, artist => artist.GroupId, group => group.Id, (artist, group) => { return(new { Group = group.GroupName, Hometown = artist.Hometown }); }) .Where(artist => artist.Hometown != "New York City"); Dictionary <string, string> notFromNY = new Dictionary <string, string>(); foreach (var artist in artistsWithGroup) { if (!notFromNY.ContainsKey(artist.Group)) { notFromNY.Add(artist.Group, artist.Group); // Console.WriteLine(artist.Group); } } // foreach (var artist in artistsWithGroup) // { // Console.WriteLine($"Group: {artist.Group}, Hometown: {artist.Hometown}"); // } //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //========================================================= var wuTang = Artists.Join(Groups, artist => artist.GroupId, group => group.Id, (artist, group) => { return(new { Group = group.GroupName, Artist = artist.ArtistName }); }) .Where(artist => artist.Group == "Wu-Tang Clan"); foreach (var artist in wuTang) { Console.WriteLine(artist.Artist); } }
public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? //========================================================= var towns = from match in Artists where match.Hometown == "Mount Vernon" select new { match.RealName, match.Age, match.Hometown }; foreach (var a in towns) { System.Console.WriteLine(a); } //========================================================= //Who is the youngest artist in our collection of artists? //========================================================= var youngest = from match in Artists orderby match.Age ascending select new { match.RealName, match.Age }; System.Console.WriteLine(youngest.First()); //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= var william = from match in Artists where match.RealName.Contains("William") select new { match.RealName }; foreach (var name in william) { System.Console.WriteLine(name); } //========================================================= //Display all groups with names less than 8 characters //========================================================= var groupname = from match in Groups select new { match.GroupName }; foreach (var gname in groupname) { int count = 0; for (int i = 0; i < gname.GroupName.Length; i++) { count++; } if (count < 8) { System.Console.WriteLine(gname.GroupName); } } //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= var altanta = from match in Artists where match.Hometown == "Atlanta" orderby match.Age descending select new { match.RealName, match.Age, match.Hometown }; int atlcount = 0; foreach (var name in altanta) { atlcount++; if (atlcount > 3) { break; } System.Console.WriteLine(name); } //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= // var groupny = from match in Groups join Artists in Group on Groups.ID equals Artists.ID select new //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //========================================================= }
public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); //========================================================= //Solve all of the prompts below using various LINQ queries //========================================================= //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? //========================================================= // var localArtist = from match in Artists // where match.Hometown == "Mount Vernon" // select new {match.ArtistName, match.Age}; // Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject( // localArtist, Formatting.Indented )); var localArtist = Artists.Where(match => match.Hometown == "Mount Vernon"); //========================================================= //Who is the youngest artist in our collection of artists? //========================================================= // var youngArtist = (from match in Artists // orderby match.Age ascending // select new {match.RealName}).First(); // Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject( youngArtist.RealName, Formatting.Indented )); // var youngArtist = Artists.OrderBy(match => match.Age).First(); //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= var williamArtists = Artists.Where(avocado => avocado.RealName.Contains("William")); //========================================================= //Display all groups with names less than 8 characters //========================================================= var eightChars = Groups.Where(avocado => avocado.GroupName.Count() < 8); //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= var oldArtists = Artists.OrderByDescending(avocado => avocado.Age).Take(3); //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //========================================================= }
public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); //========================================================= //Solve all of the prompts below using various LINQ queries //========================================================= //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? //========================================================= var foundArtist = Artists.Where(match => match.Hometown == "Mount Vernon"); Console.WriteLine("*** One artist from Mount Vernon ***"); foreach (var each in foundArtist.ToList()) { Console.WriteLine("{0} - {1}", each.ArtistName, each.Age); } //========================================================= //Who is the youngest artist in our collection of artists? //========================================================= var foundYoungest = Artists.OrderBy(match => match.Age).First(); Console.WriteLine("*** Youngest artist ***"); Console.WriteLine("{0} - {1}", foundYoungest.ArtistName, foundYoungest.Age); //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= var foundWilliams = Artists.Where(musicians => musicians.RealName.Contains("William")); Console.WriteLine("*** All artists with 'William' somewhere in their real name ***"); foreach (var each in foundWilliams.ToList()) { Console.WriteLine("{0} - {1}", each.RealName, each.Age); } //========================================================= //Display all groups with names less than 8 characters //========================================================= var foundBands = Groups.Where(match => match.GroupName.Length < 8); Console.WriteLine("*** All groups with names less than 8 characters ***"); foreach (var each in foundBands.ToList()) { Console.WriteLine(each.GroupName); } //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= var foundOldest = from match in Artists where match.Hometown == "Atlanta" orderby match.Age descending select new { match.ArtistName, match.Age }; Console.WriteLine("*** The 3 oldest artists from Atlanta ***"); for (int i = 0; i < 3; i++) { Console.WriteLine(foundOldest.ToList()[i]); } //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= HashSet <string> bands = Artists.Where(artist => artist.Hometown != "New York City").Join(Groups, artistItem => artistItem.GroupId, groupItem => groupItem.Id, (artistItem, groupItem) => { return(groupItem.GroupName); }).ToHashSet(); Console.WriteLine("***All groups that have at least one member not from New York City***"); foreach (var each in bands) { Console.WriteLine(each); } //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //========================================================= List <string> members = Groups.Where(group => group.GroupName == "Wu-Tang Clan").Join(Artists, groupItem => groupItem.Id, artistItem => artistItem.GroupId, (groupItem, artistItem) => { return(artistItem.ArtistName); }).ToList(); Console.WriteLine("***All members of the group 'Wu-Tang Clan***"); foreach (var each in members) { Console.WriteLine(each); } }
public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); //========================================================= //Solve all of the prompts below using various LINQ queries //========================================================= //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? //========================================================= object person = (from a in Artists where a.Hometown == "Mount Vernon" select a).SingleOrDefault(); System.Console.WriteLine((person as Artist).RealName + " " + (person as Artist).Age); //========================================================= //Who is the youngest artist in our collection of artists? //========================================================= int minVal = Artists.Select(x => x.Age).Min(); object personMin = (from a in Artists where a.Age == minVal select a).SingleOrDefault(); System.Console.WriteLine((personMin as Artist).RealName + " " + (personMin as Artist).Age); //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= List <Artist> wills = Artists.Where(a => a.RealName.Contains("William")).ToList(); for (int i = 0; i < wills.Count; i++) { System.Console.WriteLine(wills[i].RealName + " " + wills[i].Age); } //========================================================= //Display all groups with names less than 8 characters //========================================================= List <Group> lessThan = Groups.Where(g => g.GroupName.Length < 8).ToList(); for (int i = 0; i < lessThan.Count; i++) { System.Console.WriteLine(lessThan[i].GroupName); } //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= List <Artist> oldArt = Artists.OrderBy(a => a.Age).TakeLast(3).ToList(); for (int i = 0; i < oldArt.Count; i++) { System.Console.WriteLine(oldArt[i].RealName + " " + oldArt[i].Age); } //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //========================================================= }
public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); Console.WriteLine("###################################################################################"); //========================================================= //Solve all of the prompts below using various LINQ queries //========================================================= var theArtist = Artists.Where(x => x.Hometown == "Mount Vernon"); foreach (var artist in theArtist) { Console.Write(artist.ArtistName); Console.Write(artist.Age); } Console.WriteLine("###################################################################################"); //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? $$$$$$$$$$$$$$$$$$ DMX - 46 //========================================================= //========================================================= //Who is the youngest artist in our collection of artists? $$$$$$$$$$$$$$$$$ Chance the Rapper - 23 //========================================================= var youngartist = Artists.OrderBy(data => data.Age).First(); Console.WriteLine(youngartist.ArtistName + "" + youngartist.Age); Console.WriteLine("###################################################################################"); //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= var william = Artists.Where(x => x.RealName.Contains("William")); foreach (var x in william) { Console.WriteLine(x.ArtistName); } Console.WriteLine("###################################################################################"); //========================================================= //Display all groups with names less than 8 characters //========================================================= var lessName = Artists.Where(x => x.ArtistName.Length < 8); foreach (var y in lessName) { Console.WriteLine(y.ArtistName); } Console.WriteLine("###################################################################################"); //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= var oldAtlanta = Artists.OrderByDescending(x => x.Age).Where(y => y.Hometown == "Atlanta").ToList(); for (var i = 0; i < 3; i++) { Console.WriteLine(oldAtlanta[i].ArtistName); } Console.WriteLine("###################################################################################"); //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //========================================================= }
public static void Main(string[] args) { //Collections to work with List <Artist> Artists = JsonToFile <Artist> .ReadJson(); List <Group> Groups = JsonToFile <Group> .ReadJson(); //========================================================= //Solve all of the prompts below using various LINQ queries //========================================================= //========================================================= //There is only one artist in this collection from Mount //Vernon. What is their name and age? //========================================================= var FindArtistInVernon = Artists.Where(a => a.Hometown == "Mount Vernon"); foreach (var artist in FindArtistInVernon) { Console.WriteLine("Name: {0} " + "\n" + "Age: {1}" + "\n" + "Hometown: {2}", artist.RealName, artist.Age, artist.Hometown); } //========================================================= //Who is the youngest artist in our collection of artists? //========================================================= var FindMinAge = Artists.OrderBy(a => a.Age).Take(1); foreach (var match in FindMinAge) { System.Console.WriteLine(match.Age); } //========================================================= //Display all artists with 'William' somewhere in their //real name //========================================================= var FindWilliam = Artists.Where(a => a.RealName.Contains("William")); foreach (var match in FindWilliam) { System.Console.WriteLine(match.RealName); } //========================================================= //Display all groups with names less than 8 characters //========================================================= var Group8Char = Groups.Where(g => g.GroupName.Length < 8); foreach (var match in Group8Char) { System.Console.WriteLine(match.GroupName); } //========================================================= //Display the 3 oldest artists from Atlanta //========================================================= var AtlantaArtists = Artists.Where(a => a.Hometown == "Atlanta").OrderByDescending(a => a.Age).Take(3); foreach (var artist in AtlantaArtists) { System.Console.WriteLine("Name: {0} " + "\n" + "Age: {1}" + "\n" + "Hometown: {2}", artist.RealName, artist.Age, artist.Hometown); } //========================================================= //(Optional) Display the Group Name of all groups that have //at least one member not from New York City //========================================================= //========================================================= //(Optional) Display the artist names of all members of the //group 'Wu-Tang Clan' //========================================================= }