public static void OutputAllNeighborhoods(Manhatten manhatten) { var results = from n in manhatten.Features where n.Properties.Neighborhood != null select n.Properties.Neighborhood; Console.WriteLine("Printing off every neighborhood in Manhatten"); foreach (var neighborhood in results) { Console.WriteLine(neighborhood); } Console.WriteLine("Press any key to see all neighborhoods with a name"); Console.ReadLine(); var noEmptyNames = results.Where(n => n != ""); Console.WriteLine("Printing off every neighborhood with a name in Manhatten"); foreach (var neighborhood in noEmptyNames) { Console.WriteLine(neighborhood); } Console.WriteLine("Press any key to see all unique neighborhoods"); Console.ReadLine(); var uniqueNeighborhoods = noEmptyNames.Distinct(); Console.WriteLine("Printing off every neighborhood with a unique name in Manhatten"); foreach (var neighborhood in uniqueNeighborhoods) { Console.WriteLine(neighborhood); } }
public static Manhatten CreateManhatten() { string path = "../../../data.json"; string json = File.ReadAllText(path); Manhatten manhatten = Newtonsoft.Json.JsonConvert.DeserializeObject <Manhatten>(json); return(manhatten); }
static void Main(string[] args) { Manhatten manhatten = CreateManhatten(); OutputAllNeighborhoods(manhatten); Console.WriteLine("Press any button to query all the previoius queries in one"); Console.ReadLine(); OutputAllNeighborhoodsWithUniqueNames(manhatten); }
/// <summary> /// Method deserializes a JSON file and then filters the data /// </summary> static void JsonConversion() { string path = "../../../data.json"; string text = ""; using (StreamReader sr = File.OpenText(path)) { text = sr.ReadToEnd(); } Manhatten data = JsonConvert.DeserializeObject <Manhatten>(text); Console.WriteLine("=========== All Neighborhoods ==========="); var neighborhoods = data.features.Select(x => x).Select(x => x.properties).Select(x => x.neighborhood); foreach (var item in neighborhoods) { Console.WriteLine(item); } Console.WriteLine("=========== All Neighborhoods That Don't Have Blank ==========="); var neighborhoodsNoBlanks = neighborhoods.Where(x => x != ""); foreach (var item in neighborhoodsNoBlanks) { Console.WriteLine(item); } Console.WriteLine("=========== All Neighborhoods That Don't Have Duplicates ==========="); var neighborhoodsNoDuplicates = neighborhoodsNoBlanks.Select(x => x).Distinct(); foreach (var item in neighborhoodsNoDuplicates) { Console.WriteLine(item); } Console.WriteLine("=========== All Filters in One ==========="); var neighborhoodsAllFilters = data.features.Select(x => x).Select(x => x.properties).Select(x => x.neighborhood) .Where(x => x != "") .Select(x => x).Distinct(); foreach (var item in neighborhoodsAllFilters) { Console.WriteLine(item); } Console.WriteLine("=========== LINQ Query ==========="); var neighborhoodsNoBlanksLINQQuery = from hood in neighborhoods where hood != "" select hood; foreach (var item in neighborhoodsNoBlanksLINQQuery) { Console.WriteLine(item); } }
public static void OutputAllNeighborhoodsWithUniqueNames(Manhatten manhatten) { var results = manhatten.Features.Where(n => n.Properties.Neighborhood != null) .Where(n => n.Properties.Neighborhood != "") .GroupBy(g => g.Properties.Neighborhood) .Select(m => m.First()); ; Console.WriteLine("Printing off every neighborhood with a unique name in Manhatten"); foreach (var neighborhood in results) { Console.WriteLine(neighborhood.Properties.Neighborhood); } }