private static void Main()
        {
            var context = new GeographyEntities();
            var monasteries =
                context.Countries.Where(c => c.Monasteries.Any()).OrderBy(c => c.CountryName).Select(c => new
                {
                    c.CountryName,
                    Monasteries =
                        context.Monasteries.Where(m => m.CountryCode == c.CountryCode)
                            .OrderBy(m => m.Name)
                            .Select(m => m.Name)
                }).ToList();
            XElement XMonasteries = new XElement("monasteries");
            foreach (var country in monasteries)
            {
                XElement XCountry = new XElement("country", new XAttribute("name", country.CountryName));

                foreach (var monastery in country.Monasteries)
                {
                    XCountry.Add(new XElement("monastery", monastery));
                }
                XMonasteries.Add(XCountry);
            }
            XMonasteries.Save("../../monasteries.xml");
        }
コード例 #2
0
 static void Main()
 {
     var context = new GeographyEntities();
     var continents = context.Continents.Select(c => c.ContinentName);
     foreach (var continent in continents)
     {
         Console.WriteLine(continent);
     }
 }
コード例 #3
0
 static void Main()
 {
     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 json = jsSerializer.Serialize(rivers.ToList());
     System.IO.File.WriteAllText("../../rivers.json", json);
     //Console.WriteLine(json);
 }
        private static void Main()
        {
            var context = new GeographyEntities();
            var doc = XDocument.Load(@"..\\..\\rivers.xml");
            var riverNodes = doc.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;
                var river = new River()
                {
                    RiverName = riverName,
                    Length = riverLength,
                    Outflow = riverOutflow
                };
                try
                {
                    int riverDrainageArea = int.Parse(riverNode.Element("drainage-area").Value);
                    int riverAverageDischarge = int.Parse(riverNode.Element("average-discharge").Value);
                    river.AverageDischarge = riverAverageDischarge;
                    river.DrainageArea = riverDrainageArea;
                }
                catch (NullReferenceException nullRefEx)
                {

                }
                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.Rivers.Add(river);
                context.SaveChanges();
            }
        }