static void Main(string[] args)
        {
            // Ensure date formatting will use the English names
            Encoding asciiEncoding = Encoding.UTF8;


            var context = new PhotographySystemEntities();

            var photographs = context.Photographs
                .Select(p => new
                {
                    PhotographTitle = p.Title,
                    Category = p.Category.Name,
                    CameraLink = p.Link,
                    CameraPixelsAttribute = p.Equipment.Camera.Megapixels == null ? String.Empty : p.Equipment.Camera.Megapixels.Value.ToString(),
                    CameraManAndModel = p.Equipment.Camera.Manufacturer.Name + " " + p.Equipment.Camera.Model,
                    LensModel = p.Equipment.Lens.Manufacturer.Name+" "+ p.Equipment.Lens.Model,
                    LensPriceAttribute = p.Equipment.Lens.Price
                }).OrderBy(p=>p.PhotographTitle).ToList();

            var photographsXML = new XElement("photographs");

            foreach (var photograph in photographs)
            {
                var photographXML = new XElement("photograph",
                    new XAttribute("title", photograph.PhotographTitle),
                    new XElement("category", photograph.Category),
                    new XElement("link", photograph.CameraLink)
                    );
                var equipment = new XElement("equipment");
                
                    var cameraXML = new XElement("camera",photograph.CameraManAndModel);
                    if (photograph.CameraPixelsAttribute != null)
                    {
                        cameraXML.Add(new XAttribute("megapixels",photograph.CameraPixelsAttribute));
                    }

                    var lens = new XElement("lens",photograph.LensModel);
                    if (photograph.LensPriceAttribute != null)
                    {

                        lens.Add(new XAttribute("price", string.Format("{0:f2}", photograph.LensPriceAttribute)));
                    }
                    equipment.Add(cameraXML);
                    equipment.Add(lens);
                photographXML.Add(equipment);

                photographsXML.Add(photographXML);
            }
            Console.WriteLine(photographsXML);
            photographsXML.Save("../../photographs.xml");
        }
        static void Main(string[] args)
        {

            var context = new PhotographySystemEntities();
            var cameras = context.Cameras
                .Select(x => x.Manufacturer.Name + " " + x.Model)
                .OrderBy(x => x);
            Console.WriteLine(string.Join("\n", cameras)); // replace the foreach loop 

            //foreach (var camera in cameras)
            //{
            //    Console.WriteLine(camera);
            //}
        }
        static void Main(string[] args)
        {
            var context = new PhotographySystemEntities();
            var cameras = context.Cameras
                          .Select(x => x.Manufacturer.Name + " " + x.Model)
                          .OrderBy(x => x);

            Console.WriteLine(string.Join("\n", cameras)); // replace the foreach loop

            //foreach (var camera in cameras)
            //{
            //    Console.WriteLine(camera);
            //}
        }
 static void Main()
 {
     var context = new PhotographySystemEntities();
     var manifacturers = context.Manufacturers.OrderBy(m => m.Name).Select(m => new
     {
         m.Name,
         Cameras = context.Cameras.Where(c=>c.ManufacturerId==m.Id).OrderBy(c => c.Model).Select(c => new
         {
             c.Model,
             c.Price
         })
     }).ToList();
     var ser = new JavaScriptSerializer();
     var json = ser.Serialize(manifacturers);
     System.IO.File.WriteAllText("../../manufactureres-and-cameras.json", json);
 }
 static void Main()
 {
     var context = new PhotographySystemEntities();
     var manifacturers = context.Manufacturers.OrderBy(m=>m.Name).Select(m => new
     {
         m.Name,
         Cameras= context.Cameras.Where(c=>c.ManufacturerId==m.Id).OrderBy(c=>c.Model).Select(c=>c.Model)
     });
     foreach (var manifacturer in manifacturers)
     {
         Console.WriteLine("Company: {0}", manifacturer.Name);
         foreach (var camera in manifacturer.Cameras)
         {
             Console.Write(camera+"'");
         }
         Console.WriteLine();
     }
 }
        static void Main(string[] args)
        {
            var context = new PhotographySystemEntities();

            var manufcators = context.Manufacturers
                .Select(m => new
                {
                    manufacturer = m.Name,
                    cameras = m.Cameras.OrderBy(c => c.Model).Select(c => new
                   {
                       model = c.Model,
                       price = c.Price
                   }),
                }).OrderBy(m => m.manufacturer);
            // to use the jsSerializer we must import System.Web.Extensions from references
            var json = new JavaScriptSerializer().Serialize(manufcators);
            Console.WriteLine(json);
            System.IO.File.WriteAllText(@"../../manufactureres-and-cameras.json", json);
        }
        private static void Main()
        {
            var context = new PhotographySystemEntities();
            var photographs = context.Photographs.OrderBy(p => p.Title).Select(p => new
            {
                p.Title,
                p.Link,
                CameraModel = p.Equipment.Camera.Model,
                Megapixels = p.Equipment.Camera.Megapixels,
                LensModel = p.Equipment.Lens.Model,
                LensPrice=p.Equipment.Lens.Price,
                Category = p.Category.Name
            }).ToList();

            XElement photographsElement = new XElement("photographs");
            foreach (var photograph in photographs)
            {
                XElement xphotographs = new XElement("photograph", new XAttribute("title", photograph.Title),
                    new XElement("category", photograph.Category),
                    new XElement("link", photograph.Link),
                    new XElement("equipment",
                        new XElement("camera", photograph.CameraModel,
                            new XAttribute("megapixels", photograph.Megapixels))
                        ));
                if (photograph.LensPrice != null)
                {
                    double price =Double.Parse(photograph.LensPrice.ToString());
                    xphotographs.Add(new XElement("lens", new XAttribute("price", Math.Round(price,2).ToString("#.00")),
                        photograph.LensModel));
                }
                else
                {
                    xphotographs.Add(new XElement("lens", photograph.LensModel));
                }
                photographsElement.Add(xphotographs);
            }
            photographsElement.Save("../../photographs.xml");
        }
        private static void Main()
        {
            var context = new PhotographySystemEntities();
            var doc = XDocument.Load(@"..\\..\\manufacturers-and-lenses.xml");
            var photographyNodes = doc.XPathSelectElements("/manufacturers-and-lenses/manufacturer");
            int index = 1;
            foreach (var photographyNode in photographyNodes)
            {
                Console.WriteLine("Processing manufacturer #{0} ...", index++);
                string manifacturer = photographyNode.Element("manufacturer-name").Value;
                var query = context.Manufacturers.Where(m=>m.Name==manifacturer).Select(m => m.Name);
                if (!query.Any())
                {
                    context.Manufacturers.Add(new Manufacturer()
                    {
                        Name = manifacturer
                    });
                    context.SaveChanges();
                    Console.WriteLine("Created manufacturer: {0}", manifacturer);
                }
                else
                {
                    Console.WriteLine("Existing manufacturer: {0}", manifacturer);
                }
                var manifacturerId = context.Manufacturers.Where(m => m.Name == manifacturer).Select(m => m.Id).FirstOrDefault();
                var lensNodes = photographyNode.XPathSelectElements("lenses/lens");
                if (lensNodes.Count()!=0)
                {
                    foreach (var lensNode in lensNodes)
                    {
                        var lensModels = lensNode.Attributes("model").Select(l => l.Value);
                        string model = lensModels.FirstOrDefault().ToString();
                        var modelQuery = context.Lenses.Where(l=>l.Model==model).Select(l => l.Model);
                        if (!modelQuery.Any())
                        {
                            var lensTypes = lensNode.Attributes("type").Select(l => l.Value);
                            string type = lensTypes.FirstOrDefault().ToString();
                            if (lensNode.Attributes("price").Any())
                            {
                                var lensPrices = lensNode.Attributes("price").Select(l => l.Value);
                                decimal price = Decimal.Parse(lensPrices.FirstOrDefault().ToString());
                                context.Lenses.Add(new Lens()
                                {
                                    ManufacturerId = manifacturerId,
                                    Model = model,
                                    Type = type,
                                    Price = price
                                });
                            }
                            else
                            {
                                context.Lenses.Add(new Lens()
                                {
                                    ManufacturerId = manifacturerId,
                                    Model = model,
                                    Type = type
                                });
                            }
                            Console.WriteLine("Created lens: {0}", model);
                        }
                        else
                        {
                            Console.WriteLine("Existing lens: {0}", model);
                        }
                    }

                }
            }
            context.SaveChanges();
        }
        private static void Main(string[] args)
        {
            var context = new PhotographySystemEntities();

            XmlDocument doc = new XmlDocument();
            doc.Load("../../manufacturers-and-lenses.xml");

            var root = doc.DocumentElement;
            int id = 1;
            foreach (XmlNode manufacturer in doc.DocumentElement)
            {
                Console.WriteLine("Processing  # {0}", id++);
                var manufacturerChilds = manufacturer.ChildNodes;

                var manufacturerName = manufacturerChilds[0].InnerXml;

                Manufacturer XmlManufacturer = new Manufacturer();

                if (context.Manufacturers.Any(m => m.Name == manufacturerName))
                {
                    Console.WriteLine("Existing manufacturer: {0}", manufacturerName);
                }
                else
                {
                    Console.WriteLine("Created manufacturer: {0}",manufacturerName);
                    XmlManufacturer.Name = manufacturerName;
                    context.Manufacturers.Add(XmlManufacturer);
                    context.SaveChanges();

                }

                XmlNode lenses = manufacturerChilds[1];
                if (lenses != null)
                {
                    foreach (XmlNode lense in lenses)
                    {
                        string lenseModel = lense.Attributes["model"].Value;
                        string lenseType = lense.Attributes["type"].Value;
                        string lensePrice = lense.Attributes["price"] == null ? null : lense.Attributes["price"].Value;

                        if (context.Lenses.Any(l => l.Model == lenseModel))
                        {
                            Console.WriteLine("Existing lens: {0}", lenseModel);
                        }
                        else
                        {
                            Lens lens = new Lens()
                            {
                                Model = lenseModel,
                                Type = lenseType,
                                Price = lensePrice == null ? (decimal?)null : Decimal.Parse(lensePrice),
                                Manufacturer = context.Manufacturers.FirstOrDefault                                           (m=>m.Name==manufacturerName)
                            };
                            context.Lenses.Add(lens);
                            context.SaveChanges();
                            Console.WriteLine("Created lens: {0}",lenseModel);
                        }

                    }
                }
                Console.WriteLine();
            }
        }
        static void Main()
        {
            var context = new PhotographySystemEntities();
            var doc = XDocument.Load(@"..\\..\\generate-equipments.xml");
            var generateNodes = doc.XPathSelectElements("/generate-random-equipments/generate");
            int index = 1;
            foreach (var generateNode in generateNodes)
            {
                int count = 1;
                string manifacturer = "Nikon";
                int mId = 11;
                Console.WriteLine("Processing request #{0} ...", index++);
                if (generateNode.Attributes("generate-count").Any())
                {
                    var counts = generateNode.Attributes("generate-count").Select(l => l.Value);
                    count = int.Parse(counts.FirstOrDefault().ToString());
                }
                for (int i = 0; i < count; i++)
                {
                    if (generateNode.Element("manufacturer")!=null)
                    {
                        manifacturer = generateNode.Element("manufacturer").Value.ToString();
                        var manifacturers = context.Manufacturers.Where(m => m.Name == manifacturer).Select(m => m.Name);
                        if (!manifacturers.Any())
                        {
                            context.Manufacturers.Add(new Manufacturer()
                            {
                                Name = manifacturer
                            });
                        }
                        var query = context.Manufacturers.Where(m => m.Name == manifacturer).Select(m => m.Id);
                        mId = query.FirstOrDefault();
                    }
                    var cameras = context.Cameras.ToList();
                    Random rnd = new Random();
                    int inx = rnd.Next(cameras.Count());
                    Camera newCamera = cameras[inx];
                    newCamera.ManufacturerId = mId;

                    var lenses = context.Lenses.ToList();
                    Random r = new Random();
                    int inx2 = r.Next(lenses.Count());
                    Lens newLens = lenses[inx2];
                    newLens.ManufacturerId = mId;
                    context.Lenses.Add(newLens);
                    context.SaveChanges();

                    var lensID = context.Lenses.Where(l=>l.Model==newLens.Model).Select(l => l.Id).FirstOrDefault();
                    var cameraID = context.Cameras.Where(c => c.Model == newCamera.Model).Select(c => c.Id).FirstOrDefault();
                    context.Equipments.Add(new Equipment()
                    {
                        LensId = lensID,
                        CameraId = cameraID
                    });
                    Console.WriteLine("Equipment added: {0} (Camera: {1} - Lens: {2})",
                        manifacturer,
                        newCamera.Model,
                        newLens.Model);
                    context.SaveChanges();
                    }
            }
        }