public static void Main()
        {
            // Problem 2.	Export the Rivers as JSON
            // Write a C# application based on your EF data model for exporting all rivers along with their countries
            // in the following JSON format: 

            // [{ "riverName": "Nile", "riverLength": 6650, "countries": ["Burundi","Democratic Republic of the Congo","Egypt","Eritrea","Ethiopia","Kenya","Rwanda","South Sudan","Sudan","Tanzania","Uganda"] },
            // { "riverName": "Amazon", "riverLength": 6400, "countries": ["Bolivia","Brazil","Colombia","Ecuador","Guyana","Peru","Venezuela"] },
            // { "riverName": "Yangtze", "riverLength": 6300, "countries":["China"] },
            //  …
            // ]

            // 1. Write the output in a JSON file named rivers.json. Include in the output the rivers with no countries 
            // (if any). The JSON file code formatting is not important. - 8 score
            // 2. Order the rivers by length (from the longest) and the countries for each river alphabetically. - 3 score
            // 3. For better performance, ensure your program executes a single DB query and retrieves from the database 
            // only the required data (without unneeded rows and columns).

            using (var context = new GeographyEntities())
            {
                var rivers = context.Rivers
                    .OrderByDescending(r=>r.Length)
                    .Select(r => new
                    {
                        riverName = r.RiverName,
                        riverLength = r.Length,
                        countries = r.Countries
                        .OrderBy(c=>c.CountryName)
                        .Select(c => c.CountryName)
                    });

                var json = new JavaScriptSerializer().Serialize(rivers);
                File.WriteAllText("rivers.json", json);
            }
        }
        public static void Main()
        {
            // Problem 3.	Export Monasteries by Country as XML
            // Write a C# application based on your EF data model for exporting all monasteries by country in a XML file 
            // named monasteries.xml in the following XML format:
            // 
            // <?xml version="1.0" encoding="utf-8"?>
            // <monasteries>
            //  <country name="Bhutan">
            //   <monastery>Taktsang Palphug Monastery</monastery>
            //  </country>
            //  <country name="Bulgaria">
            //   <monastery>Bachkovo Monastery “Virgin Mary”</monastery>
            //   <monastery>Rila Monastery “St. Ivan of Rila”</monastery>
            //   <monastery>Troyan Monastery “Holy Mother's Assumption”</monastery>
            //  </country>
            // …
            // </monasteries>
            // 
            // 1. Exclude all countries with no monasteries. Use an XML parser by choice. - 8 score
            // 2. Order the countries alphabetically and the monasteries in each country also alphabetically. - 3 score
            // 3. For better performance, ensure your program executes a single DB query and retrieves from the database 
            // only the required data (without unneeded rows and columns). - 4 score

            using (var context = new GeographyEntities())
            {
                var countries = context.Countries
                    .Where(c => c.Monasteries.Any())
                    .OrderBy(c => c.CountryName)
                    .Select(c => new
                            {
                                name = c.CountryName,
                                monasteries = c.Monasteries
                                    .OrderBy(m => m.Name)
                                    .Select(m => m.Name)
                            });

                var xmlDoc = new XElement("monasteries");
                foreach (var country in countries)
                {
                    xmlDoc.Add(
                        new XElement("country",
                            new XAttribute("name", country.name),
                                from m in country.monasteries
                                select new XElement("monastery", m)));
                }

                xmlDoc.Save("monasteries.xml");
            }
        }
        public static void Main()
        {
            // Problem 1.	Entity Framework Mappings (Database First)
            // Create an Entity Framework (EF) data model of the existing database (map the database tables to C# classes).
            // Use the "database first" model in EF. To test your EF data model, list all continent names.

            using (var context = new GeographyEntities())
            {
                var continentsNames = context.Continents.Select(c => c.ContinentName);
                foreach (var continentName in continentsNames)
                {
                    Console.WriteLine(continentName);
                }
            }

            using (var context = new GeographyEntities())
            {
                var continents = context.Continents;
                foreach (var continent in continents)
                {
                    Console.WriteLine(continent.ContinentName);
                }
            }
        }
Exemple #4
0
        public static void Main()
        {
            // Problem 4.	Import Rivers from XML
            // Write a C# application based on your EF data model for importing into the DB a set of rivers given
            // in the XML file rivers.xml. 
            // The name, length and outflow elements are mandatory. The drainage-area, average-discharge and 
            // countries elements are optional.
            // 1. You should parse the XML and throw an exception in case of incorrect data, e.g. when a required 
            // element is missing or an invalid value is given. The size of the XML file will be less than 10 MB.
            // Use an XML parser by choice. - 8 score
            // 2. You should correctly import the rivers into the DB. - 7 score
            // 3. You should correctly import the countries for each river into the DB. - 5 score

            var xmlDoc = XElement.Load("../../rivers.xml");

            using (var context = new GeographyEntities())
            {
                foreach (var riverNode in xmlDoc.Elements())
                {
                    var river = new River();
                    try
                    {
                        river.RiverName = riverNode.Element("name").Value;
                        river.Length = int.Parse(riverNode.Element("length").Value);
                        river.Outflow = riverNode.Element("outflow").Value;
                        if (riverNode.Element("drainage-area") != null)
                        {
                            river.DrainageArea = int.Parse(riverNode.Element("drainage-area").Value);
                        }

                        if (riverNode.Element("average-discharge") != null)
                        {
                            river.AverageDischarge = int.Parse(riverNode.Element("average-discharge").Value);
                        }

                        if (riverNode.Descendants("country").Any())
                        {
                            foreach (var country in riverNode.Descendants("country"))
                            {
                                Console.WriteLine(country);
                                var countryCode =
                                    context.Countries.First(c => c.CountryName == country.Value);
                                river.Countries.Add(countryCode);
                            }
                        }
                    }
                    catch (NullReferenceException e)
                    {
                        throw new ArgumentNullException("Missing mandatory data in XML file");
                    }
                    catch (FormatException e)
                    {
                        throw new ArgumentException("Data provided in XML file are not in correct format");
                    }

                    context.Rivers.Add(river);
                }

                context.SaveChanges();
            }
        }