public static void ExportReportToMySQLDb() { MySQLContext db = new MySQLContext("MySQLConnStrDKostovLaptop"); IList<ProductReport> reports = ProductReportsCreator.CreateReportForEveryProductFromSQLServer(); using (db) { foreach (var report in reports) { var newReport = new sexStoreReports() { //Id = report.Id, ProductCode = report.ProductCode, Name = report.Name, SoldInShops = string.Join(" ", report.ShopNames), TotalQuantitySold = report.TotalQuantitySold, TotalIncomes = report.TotalIncomes }; db.Add(newReport); } db.SaveChanges(); } }
public static List<ProductReport> CreateReportForEveryProductFromMySQL() { MySQLContext db = new MySQLContext("MySQLConnStrDKostovLaptop"); var reports = new List<ProductReport>(); using (db) { var productsCount = GetProductsCount(db); var productSalesData = GetProductsDataFromDb(db); var firstProductId = GetFirstProductId(db); reports = new List<ProductReport>(productsCount); var length = firstProductId + productsCount; for (int i = firstProductId; i < length; i++) { var currentProduct = new ProductReport(i); foreach (var product in productSalesData) { if (product.Id == currentProduct.Id) { currentProduct.ProductCode = product.ProductCode; currentProduct.Name = product.Name; currentProduct.ShopNames.Add(product.ShopName); currentProduct.TotalQuantitySold += product.TotalQuantitySold; currentProduct.TotalIncomes += product.TotalIncomes; } } reports.Add(currentProduct); } } return reports; }
private static int GetProductsCount(MySQLContext db) { db.sexStoreReports.Count(); var productsCount = db.sexStoreReports.Count(); return productsCount; }
private static int GetFirstProductId(MySQLContext db) { var id = db.sexStoreReports.Select(x => x.Id).FirstOrDefault(); return id; }
private static List<DbDataHelpType> GetProductsDataFromDb(MySQLContext db) { var productsAndTheirSales = db.sexStoreReports .Select(r => new DbDataHelpType() { Id = r.Id, ProductCode = r.ProductCode, Name = r.Name, TotalQuantitySold = r.TotalQuantitySold, TotalIncomes = r.TotalIncomes, ShopName = r.SoldInShops }).ToList(); return productsAndTheirSales; }