static void Main(string[] args)
        {
            var context = new GeographyEntities();
            var countriesQuery = context.Countries
                .Where(c => c.Monasteries.Any())
                .OrderBy(c => c.CountryName)
                .Select(c => new
                    {
                            countryName = c.CountryName,
                            monasteries = c.Monasteries
                            .OrderBy(m => m.Name)
                            .Select(m => m.Name)
                    });

            var xmlDoc = new XDocument();
            var xmlRoot = new XElement("monasteries");
            xmlDoc.Add(xmlRoot);

            foreach (var country in countriesQuery)
            {

                var countryXml = new XElement("country", new XAttribute("name", country.countryName));
                xmlRoot.Add(countryXml);

                foreach (var monastery in country.monasteries)
                {
                    var monasteryXml = new XElement("monastery", monastery);
                    countryXml.Add(monasteryXml);
                }
            }

            Console.WriteLine(xmlDoc);
            xmlDoc.Save("monasteries.xml");

        }
        static void Main(string[] args)
        {
            var context = new GeographyEntities();
            var xmlDoc = XDocument.Load("../../rivers.xml");
            foreach (var riverElement in xmlDoc.XPathSelectElements("/rivers/river"))
            {
                var riverEntity = new River();
                riverEntity.RiverName = riverElement.Element("name").Value;
                riverEntity.Length = int.Parse(riverElement.Element("length").Value);
                riverEntity.Outflow = riverElement.Element("outflow").Value;

                if (riverElement.Element("drainage-area")!= null)
                {
                    riverEntity.DrainageArea = int.Parse(riverElement.Element("drainage-area").Value);
                }

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

                ParseAddCountriestoRiver(riverElement, context, riverEntity);

                context.Rivers.Add(riverEntity);
            }

            context.SaveChanges();

        }
        static void Main()
        {
            var context = new GeographyEntities();

            var continents = context.Continents;
            foreach (var continent in continents)
            {
                Console.WriteLine(continent.ContinentName);
            }
        }
        private static void ParseAddCountriestoRiver(XElement riverElement, GeographyEntities context, River riverEntity)
        {
            var countryElements = riverElement.XPathSelectElements("countries/country");

            foreach (var countryElement in countryElements)
            {
                var countryName = countryElement.Value;
                var countryEntity = context.Countries.Where(c => c.CountryName == countryName).FirstOrDefault();

                if (countryEntity != null)
                {
                    riverEntity.Countries.Add(countryEntity);
                }
                else
                {
                    throw new Exception("No country");
                }
            }
        }
        static void Main()
        {
            var context = new GeographyEntities();
            var rivers = context.Rivers;
            var riversQuery = 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)
                    });

            //Console.WriteLine(riversQuery);
            var json = new JavaScriptSerializer().Serialize(riversQuery);
            Console.WriteLine(json);
            File.WriteAllText("rivers.json", json);

        }