static void Main() { using (var context = new PhotographySystemEntities()) { var manufacturerCameras = context.Manufacturers .Select(m => new { manufacturer = m.Name, cameras = m.Cameras.Select(c => new { model = c.Model, price = c.Price }) .OrderBy(c => c.model) }) .OrderBy(m => m.manufacturer) .ToList(); var json = new JavaScriptSerializer().Serialize(manufacturerCameras); File.WriteAllText("../../manufacturers-and-cameras.json", json); } }
static void Main() { Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; using (var context = new PhotographySystemEntities()) { var photographsQuery = context.Photographs .Select(p => new { Title = p.Title, Category = p.Category.Name, Link = p.Link, Camera = p.Equipment.Camera.Manufacturer.Name + " " + p.Equipment.Camera.Model, CameraMegapixels = p.Equipment.Camera.Megapixels.Value, Lens = p.Equipment.Lens.Manufacturer.Name + " " + p.Equipment.Lens.Model, LensPrice = p.Equipment.Lens.Price }). OrderBy(p => p.Title) .ToList(); XElement photographs = new XElement("photographs"); foreach (var p in photographsQuery) { XElement xmlPhoto = null; if (p.LensPrice != null) { xmlPhoto = new XElement("photograph", new XAttribute("title", p.Title), new XElement("category", p.Category), new XElement("link", p.Link), new XElement("equipment", new XElement("camera", new XAttribute("megapixels", p.CameraMegapixels), p.Camera), new XElement("lens", new XAttribute("price", string.Format("{0:F2}", p.LensPrice)), p.Lens) )); } else { xmlPhoto = new XElement("photograph", new XAttribute("title", p.Title), new XElement("category", p.Category), new XElement("link", p.Link), new XElement("equipment", new XElement("camera", new XAttribute("megapixels", p.CameraMegapixels), p.Camera), new XElement("lens", p.Lens) )); } photographs.Add(xmlPhoto); } Console.WriteLine(photographs); photographs.Save("../../photographs.xml"); } }
static void Main() { var context = new PhotographySystemEntities(); }