private void AddRecordToDatabase(int productId, int quantity, decimal unitPrice, decimal sum, DateTime reportDate, string supermarketName) { using (var supermarketEntities = new SupermarketEntities()) { var report = new Report() { ProductId = productId, Quantity = quantity, UnitPrice = unitPrice, Sum = sum, ReportDate = reportDate, Supermarket = supermarketName }; supermarketEntities.Reports.Add(report); supermarketEntities.SaveChanges(); } }
private static void GenerateMongoReportsFromMSSql(MongoCollection<BsonDocument> productReports) { using (var supermarketEntities = new SupermarketEntities()) { var reportsOrederedByProductId = supermarketEntities.Reports .Include("Product.Vendor") .OrderBy(p => p.ProductId); var firstProduct = reportsOrederedByProductId.First(); int currentProductId = firstProduct.ProductId; int totalQuantitySold = 0; decimal totalIncome = 0; ProductReport productReport; Report lastReport = new Report(); foreach (var report in reportsOrederedByProductId) { if (currentProductId != report.ProductId) { productReport = new ProductReport(currentProductId, report.Product.ProductName.ToString(), report.Product.Vendor.VendorName.ToString().Trim(), totalQuantitySold, totalIncome); totalIncome = 0; totalQuantitySold = 0; currentProductId = report.ProductId; productReports.Insert(productReport); } totalIncome += report.Sum; totalQuantitySold += report.Quantity; lastReport = report; } productReport = new ProductReport(currentProductId, lastReport.Product.ProductName.ToString(), lastReport.Product.Vendor.VendorName.ToString().Trim(), totalQuantitySold, totalIncome); productReports.Insert(productReport); } }