static void Main()
        {
            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);
                }
            }

            xmlDoc.Save("monasteries.xml");
        }
示例#2
0
        static void Main(string[] args)
        {
            var context = new GeographyEntities();

            var countriesQuery = context.Countries
                .OrderBy(c => c.CountryName)
                .Where(c => c.Monasteries.Any())
                .Select(c => new
                {
                    c.CountryName,
                    Monsteries = c.Monasteries
                    .OrderBy(m => m.Name)
                    .Select(m => m.Name)
                });

            var xmlMonsteries = new XElement("monasteries");

            foreach (var country in countriesQuery)
            {
                var xmlCountry = new XElement("country");
                xmlCountry.Add(new XAttribute("name", country.CountryName));
                xmlMonsteries.Add(xmlCountry);

                foreach (var mon in country.Monsteries)
                {
                    xmlCountry.Add(new XElement("monastery", mon));
                }
            }

            var xmlDoc = new XDocument(xmlMonsteries);
            xmlDoc.Save("../../monasteries.xml");
        }
        public static void Main()
        {
            var context = new GeographyEntities();
            var rivers = context.Rivers
                .OrderByDescending(r => r.Length)
                .Select(r => new
            {
                Name = r.RiverName,
                RiverLength = r.Length,
                Countries = r.Countries
                    .OrderBy(c => c.CountryName)
                    .Select(c => c.CountryName)
            });

            //foreach (var river in rivers)
            //{
            //    Console.WriteLine("{0} - {1} - {2}", river.Name, river.RiverLength, string.Join(", ", river.Countries));
            //}

            var jsSerializer = new JavaScriptSerializer();
            var jsonRivers = jsSerializer.Serialize(rivers.ToList());
            Console.WriteLine(jsonRivers);

            File.WriteAllText(@"..\..\rivers.json", jsonRivers);
        }
        static void Main()
        {
            using (var context = new GeographyEntities())
            {
                var xmlDoc = XDocument.Load("../../rivers.xml");

                //Console.WriteLine(xmlDoc);

                var riverElements = xmlDoc.Root.Elements();
                foreach (var riverElement in riverElements)
                {
                    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);
                    }

                    ParseAndAddCountriesToRiver(riverElement, context, riverEntity);

                    context.Rivers.Add(riverEntity);
                }

                context.SaveChanges();
            }
        }
        public static void Main()
        {
            var context = new GeographyEntities();
            var countries = context.Countries;

            var countriesQuery = countries
                .Where(c => c.Monasteries.Any())
                .OrderBy(c => c.CountryName)
                .Select(c => new
                {
                    c.CountryName,
                    Monasteries = c.Monasteries
                        .OrderBy(m => m.Name)
                        .Select(m => m.Name)
                });

            // Build the output XML
            var xmlMonasteries = new XElement("monasteries");
            foreach (var country in countriesQuery)
            {
                var xmlCountry = new XElement("country");
                xmlCountry.Add(new XAttribute("name", country.CountryName));

                foreach (var monastery in country.Monasteries)
                {
                    xmlCountry.Add(new XElement("monastery", monastery));
                }

                xmlMonasteries.Add(xmlCountry);
            }
            Console.WriteLine(xmlMonasteries);

            var xmlDoc = new XDocument(xmlMonasteries);
            xmlDoc.Save(@"..\..\monasteries.xml");
        }
        static void Main()
        {
            var context = new GeographyEntities();
            var xmlDocInput = XDocument.Load("../../rivers-query.xml");
            var queryResults = new XElement("results");
            foreach (var queryElement in xmlDocInput.XPathSelectElements("/queries/query"))
            {
                var riversQuery = BuildRiversQuery(context, queryElement);
                var riversElement = new XElement("rivers");
                riversElement.Add(new XAttribute("total-count", riversQuery.Count().ToString()));
                var maxResultsAttribute = queryElement.Attribute("max-results");
                if (maxResultsAttribute != null)
                {
                    int maxResults = int.Parse(maxResultsAttribute.Value);
                    riversQuery = riversQuery.Take(maxResults);
                }
                var riverNames = riversQuery.Select(r => r.RiverName).ToList();
                foreach (var riverName in riverNames)
                {
                    riversElement.Add(new XElement("river", riverName));
                }
                riversElement.Add(new XAttribute("listed-count", riversQuery.Count().ToString()));
                queryResults.Add(riversElement);
            }

            Console.WriteLine(queryResults);
        }
 static void Main()
 {
     var context = new GeographyEntities();
     foreach (var c in context.Continents)
     {
         Console.WriteLine(c.ContinentName);
     }
 }
示例#8
0
        public static void Main()
        {
            var context = new GeographyEntities();

            foreach (var c in context.Continents)
            {
                Console.WriteLine(c.ContinentName);
            }
        }
        static void Main(string[] args)
        {
            var context = new GeographyEntities();

            foreach (var continent in context.Continents)
            {
                Console.WriteLine(continent.ContinentName);
            }
        }
        static void Main()
        {
            var context = new GeographyEntities();
            // var rivers = context.Rivers.Count(); // check DB

            // Load the XML from file and test print
            var xmlDoc = XDocument.Load(@"..\..\rivers.xml");
            //System.Console.WriteLine(xmlDoc);

            var riverNodes = xmlDoc.XPathSelectElements("/rivers/river");
            foreach (var riverNode in riverNodes)
            {
                //extract the mandatory fields
                string riverName = riverNode.Element("name").Value;
                int riverLenght = int.Parse(riverNode.Element("length").Value);
                string riverOutflow = riverNode.Element("outflow").Value;

                //extract the optional fields
                int? drainageArea = null;
                if (riverNode.Element("drainage-area") != null)
                {
                    drainageArea = int.Parse(riverNode.Element("drainage-area").Value);
                }

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

                // Import the parsed rivers into the database
                var river = new River()
                {
                    RiverName = riverName,
                    Length = riverLenght,
                    Outflow = riverOutflow,
                    DrainageArea = drainageArea,
                    AverageDischarge = averageDischarge
                };

                // Load the countries for each river
                var countryNodes = riverNode.XPathSelectElements("countries/country");
                var countryNames = countryNodes.Select(c => c.Value);
                foreach (var countryName in countryNames)
                {
                    var country = context.Countries.FirstOrDefault(c => c.CountryName == countryName);
                    river.Countries.Add(country);
                }

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

            System.Console.WriteLine("Rivers imported from rivers.xml");
        }
示例#11
0
        static void Main(string[] args)
        {
            var context = new GeographyEntities();
            var continets = context.Continents.Select(c => c.ContinentName);

            foreach (var continet in continets)
            {
                Console.WriteLine(continet);
            }
        }
示例#12
0
        static void Main()
        {
            var context = new GeographyEntities();

            // Problem 1. Entity Framework Mappings (Database First)
            // 1.9  To list all continent names
            var continents = context.Continents
                             .Select(c => c.ContinentName)
                             .ToList();

            continents.ForEach(c => Console.WriteLine("{0}", c));
        }
        static void Main()
        {
            var context = new GeographyEntities();

            XDocument xmlDoc = XDocument.Load("../../rivers.xml");

            var riversByXml =
                from river in xmlDoc.Descendants("river")
                select new
                {
                    RiverName = river.Element("name").Value,
                    Length = river.Element("length").Value,
                    Outflow = river.Element("outflow").Value,
                    DrainageArea = river.Element("drainage-area") != null ? river.Element("drainage-area").Value : null,
                    AverageDischarge = river.Element("average-discharge") != null ? river.Element("average-discharge").Value : null,
                    Countries = river.Element("countries") != null ? river.Element("countries").Elements() : null
                };

            foreach (var river in riversByXml)
            {
                var riverDb = new River();

                riverDb.RiverName = river.RiverName;
                riverDb.Length = int.Parse(river.Length);
                riverDb.Outflow = river.Outflow;

                if(!string.IsNullOrEmpty(river.DrainageArea))
                {
                    riverDb.DrainageArea = int.Parse(river.DrainageArea);
                }

                if (!string.IsNullOrEmpty(river.AverageDischarge))
                {
                    riverDb.AverageDischarge = int.Parse(river.AverageDischarge);
                }

                if (river.Countries != null)
                {
                    foreach (var country in river.Countries)
                    {
                        var countryDb = context.Countries
                            .Where(c => c.CountryName == country.Value)
                            .FirstOrDefault();

                        riverDb.Countries.Add(countryDb);
                    }
                }

                context.Rivers.Add(riverDb);
            }

            context.SaveChanges();
        }
 private static IQueryable<River> BuildRiversQuery(
     GeographyEntities context, XElement queryElement)
 {
     IQueryable<River> riversQuery = context.Rivers.AsQueryable();
     foreach (var countryElement in queryElement.XPathSelectElements("country"))
     {
         var countryName = countryElement.Value;
         riversQuery = riversQuery.Where(
             r => r.Countries.Any(c => c.CountryName == countryName));
     }
     riversQuery = riversQuery.OrderBy(r => r.RiverName);
     return riversQuery;
 }
        static void Main()
        {
            using (var context = new GeographyEntities())
            {
                var continents = context.Continents
                    .Select(c => c.ContinentName);

                foreach (var continent in continents)
                {
                    Console.WriteLine(continent);
                }
            }
        }
        static void Main(string[] args)
        {
            var context = new GeographyEntities();
            var xmlDoc = XDocument.Load(@"..\..\rivers.xml");
            var riverNodes = xmlDoc.XPathSelectElements("/rivers/river");

            foreach (var riverNode in riverNodes)
            {
                string riverName = riverNode.Element("name").Value;
                int riverLength = int.Parse(riverNode.Element("length").Value);
                string riverOutFlow = riverNode.Element("outflow").Value;

                int? drainageArea = null;
                if (riverNode.Element("drainage-area") != null)
                {
                    drainageArea = int.Parse(riverNode.Element("drainage-area").Value);
                }

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

                var countryNodes = riverNode.XPathSelectElements("countries/country");
                var countries = countryNodes.Select(c => c.Value);

                var river = new River()
                {
                    RiverName = riverName,
                    Length = riverLength,
                    Outflow = riverOutFlow,
                    DrainageArea = drainageArea,
                    AverageDischarge = averageDischarge
                };

                context.Rivers.Add(river);

                foreach (var countryName in countries)
                {
                    var country = context.Countries
                        .FirstOrDefault(c => c.CountryName == countryName);

                    river.Countries.Add(country);
                }

                context.SaveChanges();
            }
        }
        static void Main()
        {
            var context = new GeographyEntities();
            XDocument xmlDocInput = XDocument.Load("../../rivers-query.xml");
            var queryResults = new XElement("results");
            var xmlDoc = new XDocument();
            var xmlRoot = new XElement("results");
            xmlDoc.Add(xmlRoot);

            foreach (var queryElement in xmlDocInput.XPathSelectElements("/queries/query"))
            {
                var riversQuery = context.Rivers.AsEnumerable();
                foreach (var countryElement in queryElement.XPathSelectElements("country"))
                {
                    var countryName = countryElement.Value;
                    riversQuery = riversQuery.Where(
                        r => r.Countries.Any(c => c.CountryName == countryName)
                    );
                }
                var maxResultsAttribute = queryElement.Attribute("max-results");
                int maxResults = maxResultsAttribute != null ? int.Parse(maxResultsAttribute.Value) : 0;
                if (maxResultsAttribute != null)
                {
                    riversQuery = riversQuery
                        .OrderBy(r => r.RiverName);
                }
                int totalRivers = riversQuery.Count();
                riversQuery = riversQuery.Take(maxResults);
                var riverNames = riversQuery.Select(r => r.RiverName);
                var xmlRiversElement = new XElement("rivers");
                var riverTotalCountAttribute = new XAttribute("total-count", totalRivers);
                var riverListedCount = new XAttribute("listed-count", riverNames.Count());
                xmlRiversElement.Add(riverTotalCountAttribute);
                xmlRiversElement.Add(riverListedCount);
                xmlRoot.Add(xmlRiversElement);

                foreach (var river in riverNames)
                {
                    var riverXmlElement = new XElement("river", river);
                    xmlRiversElement.Add(riverXmlElement);
                }

                Console.WriteLine(xmlRoot);
            }
        }
 private static void ParseAndAddCountriesToRiver(
     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.FirstOrDefault(c => c.CountryName == countryName);
         if (countryEntity != null)
         {
             riverEntity.Countries.Add(countryEntity);
         }
         else
         {
             throw new Exception(string.Format("Cannot find country {0} in the DB", countryName));
         }
     }
 }
        static void Main(string[] args)
        {
            var context = new GeographyEntities();

            var riversQuery = 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(riversQuery);

            File.WriteAllText(@"rivers.json", json);
        }
示例#20
0
        static void Main(string[] args)
        {
            var context = new GeographyEntities();
            var rivers = context.Rivers
                .OrderByDescending(r => r.Length)
                .Select(r => new
            {
                r.RiverName,
                r.Length,
                Countries = r.Countries
                .OrderBy(c => c.CountryName)
                .Select(c => c.CountryName)
            });

            var jsSerializer = new JavaScriptSerializer();
            var riversJSON = jsSerializer.Serialize(rivers.ToList());
            Console.WriteLine(riversJSON);
            
            System.IO.File.WriteAllText(@"../../riversJSON.txt", riversJSON);
        }
        private 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);
        }
        static void Main()
        {
            using (var context = new GeographyEntities())
            {
                var riverQuery = 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)
                    });

                //Console.WriteLine(riverQuery);

                var jsonRivers = new JavaScriptSerializer().Serialize(riverQuery);

                System.IO.File.WriteAllText(@"../../rivers.json", jsonRivers);
            }
        }
        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)
                    });

            var jsSerializer = new JavaScriptSerializer();
            var riversJson = jsSerializer.Serialize(riversQuery);

            File.WriteAllText(@"../../rivers.json", riversJson);
            Console.WriteLine(@"Rivers exported to rivers.json");
        }
        public static void Main()
        {
            var context = new GeographyEntities();

            var xmlDoc = XDocument.Load(@"..\..\rivers.xml");
            var riverNodes = xmlDoc.XPathSelectElements("/rivers/river");

            foreach (var riverNode in riverNodes)
            {
                //Console.WriteLine(riverNode);
                string riverName = riverNode.Element("name").Value;
                int riverLength = int.Parse(riverNode.Element("length").Value);
                string riverOutflow = riverNode.Element("outflow").Value;

                int? drainageArea = null;
                if (riverNode.Element("drainage-area") != null)
                {
                    drainageArea = int.Parse(riverNode.Element("drainage-area").Value);
                }

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

                var river = new River()
                {
                    RiverName = riverName,
                    AverageDischarge = averageDischarge,
                    DrainageArea = drainageArea,
                    Length = riverLength,
                    Outflow = riverOutflow
                };

                context.Rivers.Add(river);
                context.SaveChanges();

                var countryNodes = riverNode.XPathSelectElements("countries/country");
                var countryNames = countryNodes.Select(c => c.Value);
                foreach (var countryName in countryNames)
                {
                    var country = context.Countries.FirstOrDefault(c => c.CountryName == countryName);
                    river.Countries.Add(country);
                }

                context.SaveChanges();
                //List<string> countries = new List<string>();
                //if (riverNode.Element("countries") != null)
                //{
                //var riverCountries = riverNode.Descendants("country");
                //int counter = 0;

                //foreach (var country in riverCountries)
                //{
                //    countries.Add(country.Value);
                //}

                //Second Way
                //}
            }
        }