public static void GetExcelData()
        {
            using (var db = new CatalogContext())
            {
                HashSet<string> addedShops = new HashSet<string>();

                //this is used for avoiding duplicates and fast search
                foreach (var item in db.Shops)
                {
                    addedShops.Add(item.Name);
                }

                foreach (var path in paths)
                {
                    foreach (var fileName in path.Value)
                    {
                        AddShop(fileName, addedShops, db);
                    }
                }

                db.SaveChanges();
              AddSale(db, paths);
                db.SaveChanges();
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            string path = @"../../file.xml";
            Dictionary<string, Dictionary<string, decimal>> data = new Dictionary<string, Dictionary<string, decimal>>();


            XmlDocument doc = new XmlDocument();
            doc.Load(@"../../file.xml");
            XmlNode rootNode = doc.DocumentElement;
            //Console.WriteLine("Root node: {0}", rootNode.Name);

            decimal expense = 0m;
            string txt = "";
            int count = 0;
            foreach (XmlNode node in rootNode.ChildNodes)
            {
                string vendor = node.Attributes["vendor"].Value.ToString();
                string month = "";
                //Console.WriteLine(vendor);
                foreach (XmlNode child in node)
                {
                    month = child.Attributes["month"].Value.ToString();
                    txt = child.InnerText.ToString();
                    expense = decimal.Parse(txt, CultureInfo.InvariantCulture);

                    if (!data.ContainsKey(vendor))
                    {
                        data[vendor] = new Dictionary<string, decimal>();
                    }
                    data[vendor].Add(month, expense);

                    VendorReport currentReport = new VendorReport(vendor, month, expense);
                    JsonClient.InsertIntoMongoVendorReport(currentReport, count);
                    count++;
                }
            }

            using (var db = new CatalogContext())
            {
                //one query to database
                var currentVendors = db.Vendors.ToList();
                var currentExpenses = db.VendorExpenses.ToList();

                foreach (var vendor in data)
                {

                    int vendorID = currentVendors
                        .Where(x => x.Name.CompareTo(vendor.Key) == 0)
                        .Select(x => x.VendorId).First();

                    foreach (var vendorExpense in vendor.Value)
                    {
                        VendorExpense newExpense = new VendorExpense
                            {
                                VendorID = vendorID,
                                Month = DateTime.Parse(vendorExpense.Key),
                                Expense = vendorExpense.Value
                            };
                        var currentExpense = currentExpenses.Where(x => x.VendorID == vendorID && x.Month == DateTime.Parse(vendorExpense.Key)).FirstOrDefault();
                        if (currentExpense != null)
                        {
                            currentExpense.Expense = vendorExpense.Value;
                        }
                        else
                        {
                            db.VendorExpenses.Add(newExpense);
                        }
                    }
                }
                db.SaveChanges();
            }
        }
Esempio n. 3
0
        public static void Initialize(CatalogContext context)
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            if (context.Teachers.Any()) //DB has been seeded
            {
                return;
            }

            var spokenLanguages = new SpokenLanguage[]
            {
                new SpokenLanguage {
                    Name = "PT"
                },
                new SpokenLanguage {
                    Name = "EN"
                }
            };

            foreach (SpokenLanguage sl in spokenLanguages)
            {
                context.SpokenLanguages.Add(sl);
            }
            context.SaveChanges();

            var teachers = new Teacher[]
            {
                new Teacher {
                    Name             = "André Alves",
                    Birthday         = new DateTime(1991, 6, 10),
                    Nacionality      = "PT",
                    AcademicalDegree = "Master",
                    //SpokenLanguages = new List<SpokenLanguage>(new SpokenLanguage[]{spokenLanguages[0], spokenLanguages[1]}),
                    PricePerHour  = 15.0,
                    TeachingAreas = "Math, History"
                },
                new Teacher {
                    Name             = "Sofia Natálio",
                    Birthday         = new DateTime(1990, 8, 25),
                    Nacionality      = "PT",
                    AcademicalDegree = "Bachelor",
                    //SpokenLanguages = new List<SpokenLanguage>(new SpokenLanguage[]{spokenLanguages[0], spokenLanguages[1]}),
                    PricePerHour  = 20.0,
                    TeachingAreas = "Programming, History, Arts"
                }
            };

            foreach (Teacher s in teachers)
            {
                context.Teachers.Add(s);
            }
            context.SaveChanges();

            var teacherSpokenLanguages = new TeacherSpokenLanguages[]
            {
                new TeacherSpokenLanguages
                {
                    Teacher        = teachers[0],
                    SpokenLanguage = spokenLanguages[0]
                },
                new TeacherSpokenLanguages
                {
                    Teacher        = teachers[1],
                    SpokenLanguage = spokenLanguages[1]
                }
            };

            foreach (TeacherSpokenLanguages tsl in teacherSpokenLanguages)
            {
                context.TeacherSpokenLanguages.Add(tsl);
            }
            context.SaveChanges();
        }