static void Main()
 {
     var context = new GeographyEntities();
     var countries = context.Countries
         .OrderBy(c => c.CountryName)
         .Select(c => new
         {
             c.CountryName,
             Monasteries = c.Monasteries
                 .OrderBy(m => m.Name)
                 .Select(m => m.Name)
         });
     var xmlRoot = new XElement("monasteries");
     foreach (var country in countries)
     {
         if (country.Monasteries.Any())
         {
             var countryXml = new XElement("country",
                 new XAttribute("name", country.CountryName));
             foreach (var monastery in country.Monasteries)
             {
                 var monasteryXml = new XElement("monastery", monastery);
                 countryXml.Add(monasteryXml);
             }
             xmlRoot.Add(countryXml);
         }
     }
     var xmlDoc = new XDocument(xmlRoot);
     xmlDoc.Save("monasteries.xml");
     Console.WriteLine(@"Monasteries exported to Bin\Debug\monasteries.xml");
 }
 static void Main()
 {
     Console.WriteLine("Continents:");
     var context = new GeographyEntities();
     foreach (var continent in context.Continents)
     {
         Console.WriteLine(continent.ContinentName);
     }
 }
    static void Main()
    {
        var xmlDoc = XDocument.Load(@"..\..\rivers.xml");
        var riverNodes = xmlDoc.XPathSelectElements("/rivers/river");
        var context = new GeographyEntities();
        foreach (var riverNode in riverNodes)
        {
            // Parse the river properties
            var river = new River
            {
                RiverName = riverNode.Descendants("name").First().Value,
                Length = int.Parse(riverNode.Descendants("length").First().Value),
                Outflow = riverNode.Descendants("outflow").First().Value
            };
            var drainageArea = riverNode.Descendants("drainage-area").FirstOrDefault();
            if (drainageArea != null)
            {
                river.DrainageArea = int.Parse(drainageArea.Value);
            }
            var averageDischarge = riverNode.Descendants("average-discharge").FirstOrDefault();
            if (averageDischarge != null)
            {
                river.AverageDischarge = int.Parse(averageDischarge.Value);
            }

            // Load the countries for each river
            var countryNodes = riverNode.XPathSelectElements("countries/country");
            foreach (var countryNode in countryNodes)
            {
                var country = context.Countries.
                    FirstOrDefault(c => c.CountryName == countryNode.Value);
                if (country == null)
                {
                    throw new Exception("Can not find country: " + countryNode.Value);
                }
                river.Countries.Add(country);
            }

            // Save the river in the database
            context.Rivers.Add(river);
            context.SaveChanges();
        }

        Console.WriteLine("Rivers imported from rivers.xml");
    }
 static void Main()
 {
     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)
         })
         .ToList();
     var jsSerializer = new JavaScriptSerializer();
     var riversJson = jsSerializer.Serialize(rivers);
     File.WriteAllText("rivers.json", riversJson);
     Console.WriteLine(@"Rivers exported to Bin\Debug\rivers.json");
 }
    static void Main()
    {
        var riversQueryXml = XDocument.Load(@"..\..\rivers-query.xml");
        var queryNodes = riversQueryXml.XPathSelectElements("/queries/query");
        var context = new GeographyEntities();
        var allResultsXml = new XElement("results");
        foreach (var queryNode in queryNodes)
        {
            var countries = queryNode.XPathSelectElements("country").Select(c => c.Value);

            // Build the "river names by given countries" query
            var riversQuery = context.Rivers.AsQueryable();
            foreach (var country in countries)
            {
                riversQuery = riversQuery.Where(
                    r => r.Countries.Any(c => c.CountryName == country));
            }

            riversQuery = riversQuery.OrderBy(r => r.RiverName);
            var riverNamesQuery = riversQuery.Select(r => r.RiverName);

            // Build the query results
            var totalCount = riverNamesQuery.Count();
            var maxResults = queryNode.Attribute("max-results");
            if (maxResults != null)
            {
                riverNamesQuery = riverNamesQuery.Take(int.Parse(maxResults.Value));
            }
            var riverNames = riverNamesQuery.ToList();
            var listedCount = riverNames.Count();

            // Build the query result XML
            var resultXml = new XElement("rivers",
                new XAttribute("total-count", totalCount),
                new XAttribute("listed-count", listedCount),
                riverNames.Select(river => new XElement("river", river)));

            allResultsXml.Add(resultXml);
        }
        Console.WriteLine(allResultsXml);
    }