From 7abacc9c1432c0383cb377d65f9e00b0facec4ba Mon Sep 17 00:00:00 2001 From: Sascha Date: Thu, 24 Aug 2023 08:50:38 +0200 Subject: [PATCH] replace foreach with for loop --- .../Data/EntityClasses/Product.cs | 4 ++-- .../Services/GenericImporter.cs | 23 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Gremlin_BlazorServer/Data/EntityClasses/Product.cs b/Gremlin_BlazorServer/Data/EntityClasses/Product.cs index 99a0bf4..2828baa 100644 --- a/Gremlin_BlazorServer/Data/EntityClasses/Product.cs +++ b/Gremlin_BlazorServer/Data/EntityClasses/Product.cs @@ -15,8 +15,8 @@ public class Product : IMetadata { public uint CustomDescriptionId { get; set; } //Agilent-specific properties: - public string? ProductNumber { get; set; } - public string? OptionNumber { get; set; } + public string ProductNumber { get; set; } = string.Empty; + public string OptionNumber { get; set; } = string.Empty; public string? SapShortDescription { get; set; } public string? SapLongDescription { get; set; } public float Weight { get; set; } diff --git a/Gremlin_BlazorServer/Services/GenericImporter.cs b/Gremlin_BlazorServer/Services/GenericImporter.cs index e8b4cbc..244f170 100644 --- a/Gremlin_BlazorServer/Services/GenericImporter.cs +++ b/Gremlin_BlazorServer/Services/GenericImporter.cs @@ -8,8 +8,9 @@ public class GenericImporter { Console.WriteLine($"GENERIC IMPORTER: Importing {typeof(T)} from csv..."); List splitLines = SplitLines(fileContent); Console.WriteLine($"Found {splitLines.Count} potential {typeof(T)} in csv."); - int i = await ParseLinesToResultSet(splitLines); - Console.WriteLine(i > 0 ? $"GENERIC IMPORTER: wrote {i} to db." : "GENERIC IMPORTER: Error by writing to db!"); + int result = await ParseLinesToResultSet(splitLines); + if (result == -1) return; //no updates or new items + Console.WriteLine(result > 0 ? $"GENERIC IMPORTER: wrote {result} to db." : "GENERIC IMPORTER: Error by writing to db!"); } private static List SplitLines(string fileContent) where T : class, IMetadata, new() { @@ -58,20 +59,18 @@ public class GenericImporter { List updatedItems = new(); List newItems = new(); IList allItems = await GenericController.GetAllAsync() ?? new List(); - int i = 0; - IEnumerable lines = lineList.Select(strings => strings.Select(x => x.Replace("\"", string.Empty)).ToArray()).ToList(); - foreach (string[] line in lines) { - i++; - if (typeof(T) == typeof(Account)) resultSet = ParseToAccount(line, allItems as IList) as Tuple; - if (typeof(T) == typeof(Contact)) resultSet = ParseToContact(line, allItems as IList) as Tuple; - if (typeof(T) == typeof(Product)) resultSet = ParseToProduct(line, allItems as IList) as Tuple; - if (typeof(T) == typeof(CustomDescription)) resultSet = ParseToCustomDescription(line, allItems as IList) as Tuple; + List lines = lineList.Select(strings => strings.Select(x => x.Replace("\"", string.Empty)).ToArray()).ToList(); + for (int i = 0; i < lines.Count; i++) { + if (typeof(T) == typeof(Account)) resultSet = ParseToAccount(lines[i], allItems as IList) as Tuple; + if (typeof(T) == typeof(Contact)) resultSet = ParseToContact(lines[i], allItems as IList) as Tuple; + if (typeof(T) == typeof(Product)) resultSet = ParseToProduct(lines[i], allItems as IList) as Tuple; + if (typeof(T) == typeof(CustomDescription)) resultSet = ParseToCustomDescription(lines[i], allItems as IList) as Tuple; if (resultSet is null) continue; if (resultSet.Item1 is not null) updatedItems.Add(resultSet.Item1); if (resultSet.Item2 is not null) newItems.Add(resultSet.Item2); - DrawTextProgressBar(i, lines.Count()); + DrawTextProgressBar(i, lines.Count); } int success = 0; @@ -79,7 +78,7 @@ public class GenericImporter { Console.WriteLine($"Found {updatedItems.Count} updates and {newItems.Count} new items."); if (updatedItems.Count > 0) success += GenericController.Update(updatedItems); if (newItems.Count > 0) success += GenericController.Insert(newItems); - + if (updatedItems.Count == 0 && newItems.Count == 0) return -1; return success; }