private static void ProcessLenses(IEnumerable<XElement> lenses, Manufacturer manu, PhotographySystemEntities context) { foreach (var lens in lenses) { var lensModel = lens.Attribute("model").Value; var lensType = lens.Attribute("type").Value; var xlensPrice = lens.Attribute("price"); string lensPrice = null; if (xlensPrice != null) { lensPrice = xlensPrice.Value; } var newLens = context.Lenses.FirstOrDefault(l => l.Model == lensModel); if (newLens == null) { newLens = new Lens {Model = lensModel, Type = lensType}; if (lensPrice != null) { newLens.Price = decimal.Parse(lensPrice); } context.Lenses.Add(newLens); manu.Lenses.Add(newLens); context.SaveChanges(); Console.WriteLine("Created lens: {0}", lensModel); } else { Console.WriteLine("Existing lens: {0}", lensModel); } } }
static void Main() { var context = new PhotographySystemEntities(); var doc = XDocument.Load("../../imports.xml"); var manufacturers = doc.XPathSelectElements("/manufacturers-and-lenses/manufacturer"); int process = 1; foreach (var manufacturer in manufacturers) { Console.WriteLine("Processing manufacturer #{0} ...", process); Manufacturer manu = ProcessManufacturer(manufacturer, context); var lenses = manufacturer.XPathSelectElements("lenses/lens"); ProcessLenses(lenses, manu, context); Console.WriteLine(); } }
private static Manufacturer ProcessManufacturer(XElement manufacturer, PhotographySystemEntities context) { var manuName = manufacturer.Element("manufacturer-name").Value; var manu = context.Manufacturers.FirstOrDefault(m => m.Name == manuName); if (manu == null) { manu = new Manufacturer {Name = manuName}; context.Manufacturers.Add(manu); context.SaveChanges(); Console.WriteLine("Created manufacturer: {0}", manuName); } else { Console.WriteLine("Existing manufacturer: {0}", manuName); } return manu; }
private static void GenRndEquip(XmlSpec xmlSpec, PhotographySystemEntities context) { Random rnd = new Random(); for (int i = 0; i < xmlSpec.Count; i++) { var equipment = new Equipment(); var lenses = context.Lenses.Where(l => l.Manufacturer.Name == xmlSpec.ManuName).Select(l => l).ToList(); var cameras = context.Cameras.Where(c => c.Manufacturer.Name == xmlSpec.ManuName).Select(c => c).ToList(); equipment.Lens = lenses.ElementAt(rnd.Next(lenses.Count())); equipment.Camera = cameras.ElementAt(rnd.Next(cameras.Count())); context.Equipments.Add(equipment); context.SaveChanges(); Console.WriteLine("Equipment added: {0} (Camera: {1} - Lens: {2})", xmlSpec.ManuName, equipment.Camera.Model, equipment.Lens.Model); } }
static void Main() { var context = new PhotographySystemEntities(); var photos = context.Photographs .Select(p => new { title = p.Title, category = p.Category.Name, link = p.Link, cameraManu = p.Equipment.Camera.Manufacturer.Name, cameraModel = p.Equipment.Camera.Model, cameraPxs = p.Equipment.Camera.Megapixels, lensManu = p.Equipment.Lens.Manufacturer.Name, lensModel = p.Equipment.Lens.Model, lensPrice = p.Equipment.Lens.Price }).OrderBy(p => p.title) .ToList(); var root = new XElement("photographs"); foreach (var ph in photos) { var xPhoto = new XElement("photograph"); xPhoto.Add(new XAttribute("title", ph.title)); xPhoto.Add(new XElement("category", ph.category)); xPhoto.Add(new XElement("link", ph.link)); var xEquip = new XElement("equipment"); xEquip.Add(new XElement("camera", ph.cameraManu + " " + ph.cameraModel, new XAttribute("megapixels", ph.cameraPxs))); var lens = new XElement("lens", ph.lensManu + " " + ph.lensModel); if (ph.lensPrice != null) { lens.Add(new XAttribute("price", ph.lensPrice)); } xEquip.Add(lens); xPhoto.Add(xEquip); root.Add(xPhoto); } var doc = new XDocument(); doc.Add(root); doc.Save("../../photographs.xml"); }
public static void Main() { var context = new PhotographySystemEntities(); var listManufacModel = context.Manufacturers .Include(m => m.Cameras) .Select(m => new { manu = m.Name, cam = m.Cameras.Select(c => c.Model).OrderBy(c => c) }) .OrderBy(mc => mc.manu) .ToList(); foreach (var mc in listManufacModel) { foreach (var model in mc.cam) { Console.WriteLine("{0} {1}", mc.manu, model ); } } }
static void Main() { var context = new PhotographySystemEntities(); var doc = XDocument.Load("../../generate-equipment.xml"); var generates = doc.XPathSelectElements("/generate-random-equipments/generate"); int process = 1; foreach (var generate in generates) { Console.WriteLine("Processing manufacturer #{0} ...", process); var xmlSpec = new XmlSpec(); if (generate.Element("manufacturer") != null) { xmlSpec.ManuName = generate.Element("manufacturer").Value; } if (generate.Attribute("generate-count") != null) { xmlSpec.Count = int.Parse(generate.Attribute("generate-count").Value); } GenRndEquip(xmlSpec, context); Console.WriteLine(); } }
static void Main() { var context = new PhotographySystemEntities(); var manuAndCams = context.Manufacturers .Include(m => m.Cameras) .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 serializer = new JavaScriptSerializer(); var json = serializer.Serialize(manuAndCams); File.WriteAllText("../../manufactureres-and-cameras.json", json); }