replace foreach with for loop

pull/3/head
Sascha 2023-08-24 08:50:38 +07:00
parent 3b3a8e3903
commit 7abacc9c14
2 changed files with 13 additions and 14 deletions

@ -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; }

@ -8,8 +8,9 @@ public class GenericImporter {
Console.WriteLine($"GENERIC IMPORTER: Importing {typeof(T)} from csv...");
List<string[]> splitLines = SplitLines<T>(fileContent);
Console.WriteLine($"Found {splitLines.Count} potential {typeof(T)} in csv.");
int i = await ParseLinesToResultSet<T>(splitLines);
Console.WriteLine(i > 0 ? $"GENERIC IMPORTER: wrote {i} to db." : "GENERIC IMPORTER: Error by writing to db!");
int result = await ParseLinesToResultSet<T>(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<string[]> SplitLines<T>(string fileContent) where T : class, IMetadata, new() {
@ -58,20 +59,18 @@ public class GenericImporter {
List<T> updatedItems = new();
List<T> newItems = new();
IList<T> allItems = await GenericController.GetAllAsync<T>() ?? new List<T>();
int i = 0;
IEnumerable<string[]> 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<Account>) as Tuple<T?, T?>;
if (typeof(T) == typeof(Contact)) resultSet = ParseToContact(line, allItems as IList<Contact>) as Tuple<T?, T?>;
if (typeof(T) == typeof(Product)) resultSet = ParseToProduct(line, allItems as IList<Product>) as Tuple<T?, T?>;
if (typeof(T) == typeof(CustomDescription)) resultSet = ParseToCustomDescription(line, allItems as IList<CustomDescription>) as Tuple<T?, T?>;
List<string[]> 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<Account>) as Tuple<T?, T?>;
if (typeof(T) == typeof(Contact)) resultSet = ParseToContact(lines[i], allItems as IList<Contact>) as Tuple<T?, T?>;
if (typeof(T) == typeof(Product)) resultSet = ParseToProduct(lines[i], allItems as IList<Product>) as Tuple<T?, T?>;
if (typeof(T) == typeof(CustomDescription)) resultSet = ParseToCustomDescription(lines[i], allItems as IList<CustomDescription>) as Tuple<T?, T?>;
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;
}