private Dictionary <string, int> GetColumnNames(CSVColumnSettings columnSettings) { if (columnSettings.DataRows.Length == 0 || columnSettings.DataRows.Length <= columnSettings.StartRowIndex) { throw new ArgumentException($"{nameof(columnSettings.DataRows)} must no be empty"); } string[] columns = null; var settings = new CSVSettings { Separator = columnSettings.Separator, HasSurroundingQuotationMarks = columnSettings.HasSurroundingQuotationMarks }; if (columnSettings.HasColumnRow) { if (columnSettings.HasSurroundingQuotationMarks) { var columnRow = columnSettings.DataRows[columnSettings.StartRowIndex]; columns = columnRow.Split('\"').Where(part => part != columnSettings.Separator.ToString()).Skip(1).ToArray(); if (columns.Last().IsNullOrEmpty() && !columnRow.EndsWith(columnSettings.Separator.ToString())) { columns = columns.Take(columns.Length - 1).ToArray(); } } else { columns = columnSettings.DataRows[columnSettings.StartRowIndex].Replace("\n", "").Trim().Split(columnSettings.Separator); } } else { foreach (var columnRow in columnSettings.DataRows.Skip(columnSettings.StartRowIndex)) { settings.DataRow = columnRow; var dataCells = GetDataCells(settings); if (columns == null || columns.Length < dataCells.Length) { columns = dataCells; } } } var columnsDictionary = new Dictionary <string, int>(); for (var i = 0; i < columns.Length; i++) { columnsDictionary.Add(columnSettings.HasColumnRow ? columns[i] : i.ToString(), i); } return(columnsDictionary); }
public IEnumerable <T> Convert <T>(DataProperty configuration, string data, bool removeDublicates = true) where T : class, IEmpty { if (data.IsNullOrEmpty()) { return(new List <T>()); } data = data.Replace("\r", ""); var dataRows = data.Split('\n'); var dataRecords = new List <T>(); var columnSettings = new CSVColumnSettings { DataRows = dataRows, HasColumnRow = configuration.HasColumnNamesCSV, HasSurroundingQuotationMarks = configuration.HasSurroundingQuotationMarksCSV, Separator = configuration.SeparatorCSVColumn, StartRowIndex = configuration.StartRowIndexCSV }; var settings = new CSVSettings { ColumnNames = GetColumnNames(columnSettings), Properties = configuration.DataProperties, Separator = configuration.SeparatorCSVColumn, CultureInfo = configuration.CultureCode.GetCultureInfo(), HasSurroundingQuotationMarks = configuration.HasSurroundingQuotationMarksCSV }; var startRowIndex = configuration.StartRowIndexCSV + (configuration.HasColumnNamesCSV ? 1 : 0); for (var rowIndex = startRowIndex; rowIndex < dataRows.Length; rowIndex++) { settings.DataRow = dataRows[rowIndex]; var dataRecord = ProcessDataRow <T>(settings); if (!(dataRecord?.IsEmpty ?? false)) { dataRecords.Add(dataRecord); } } return(removeDublicates ? RemoveDoublets(configuration, dataRecords) : dataRecords); }