Esempio n. 1
0
        public JsonResult Compare()
        {
            Stopwatch sw = Stopwatch.StartNew();
            // Достаем товары из CSV файла
            string Path = env.WebRootPath + "\\CsvFiles\\";

            string[]        stringsCSV   = CsvWriter.ReadCSVFile("test.csv", Path);
            List <CsvModel> FileProducts = CsvWriter.FromCsvToCSVModel(stringsCSV);


            int Coincidences = 0;
            int NewPrice     = 0;

            foreach (var item in FileProducts)
            {
                var product = db.Products.FirstOrDefault(c => c.VendorRef.VendorName == item.VendorName && c.ProductName == item.ProductName);
                if (product != null)
                {
                    Coincidences++;
                    if (product.Price != item.Price)
                    {
                        product.Price = item.Price;
                        NewPrice++;
                    }
                }
            }
            try
            {
                db.SaveChanges();
                sw.Stop();
            }
            catch (Exception)
            {
                // Обработать исключения
            }

            CompareResult result = new CompareResult
            {
                Coincidences = Coincidences,
                NewPrice     = NewPrice,
                Time         = sw.Elapsed.ToString()
            };

            return(Json(result));
        }