public class GovernmentCsvRecord
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
//etc 200 properties in class
public static StringCsv StringCsvGovernmentToEntity(GovernmentCsvRecord data)
{
StringCsv entity = new StringCsv();
entity.Id = Guid.NewGuid();
entity.Col1 = data.Col1;
entity.Col2 = data.Col2;
//etc until 200 properties are filled
}
//The `IntegerCsv` entity
public static IntegerCsv IntCsvGovernmentToEntity(GovernmentCsvRecord data)
{
IntegerCsv entity = new IntegerCsv();
entity.Id = Guid.NewGuid();
entity.Col1 = data.Col1;
entity.Col2 = data.Col2;
//etc until 200 properties are filled
}
}
public class GovernmentCsvRecord
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
//etc 200 properties in class
public static StringCsv StringCsvGovernmentToEntity(GovernmentCsvRecord data)
{
StringCsv entity = new StringCsv();
entity.Id = Guid.NewGuid();
entity.Col1 = data.Col1;
entity.Col2 = data.Col2;
//etc until 200 properties are filled
}
//The `IntegerCsv` entity
public static IntegerCsv IntCsvGovernmentToEntity(GovernmentCsvRecord data)
{
IntegerCsv entity = new IntegerCsv();
entity.Id = Guid.NewGuid();
entity.Col1 = data.Col1;
entity.Col2 = data.Col2;
//etc until 200 properties are filled
}
}
public class GovernmentCsvRecord
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
//etc 200 properties in class
public static StringCsv StringCsvGovernmentToEntity(GovernmentCsvRecord data)
{
StringCsv entity = new StringCsv();
entity.Id = Guid.NewGuid();
entity.Col1 = data.Col1;
entity.Col2 = data.Col2;
//etc until 200 properties are filled
}
//The `IntegerCsv` entity
public static IntegerCsv IntCsvGovernmentToEntity(GovernmentCsvRecord data)
{
IntegerCsv entity = new IntegerCsv();
entity.Id = Guid.NewGuid();
entity.Col1 = data.Col1;
entity.Col2 = data.Col2;
//etc until 200 properties are filled
}
}
The structure of the StringCsv
and IntergerCsv
Entities are different (What I mean is that the structure of the CSV's with string's and int's are fundamentally different) , but I need to save the data to an Entity that is consistent, which means I need to check which CSV is being imported to differentiate between the methods needed to save to the database. ie, There is a date column, but it differs between the Interger
CSV and the String
CSV.
Also, I can't add the data dirrectly into the Entities StringCsv
and IntergerCsv
b/c they need an Id
which is a Guid
and the CSV file does not contain that, which is why I read to the the class GovernmentCsvRecord
and then covert the object to the Entity.
Below are samples of CSV files, sorry I can't post actual values.
Below are samples of CSV files, sorry I can't post actual values.
The structure of the StringCsv
and IntergerCsv
Entities are different (What I mean is that the structure of the CSV's with string's and int's are fundamentally different) , but I need to save the data to an Entity that is consistent, which means I need to check which CSV is being imported to differentiate between the methods needed to save to the database. ie, There is a date column, but it differs between the Interger
CSV and the String
CSV.
Also, I can't add the data dirrectly into the Entities StringCsv
and IntergerCsv
b/c they need an Id
which is a Guid
and the CSV file does not contain that, which is why I read to the the class GovernmentCsvRecord
and then covert the object to the Entity.
Below are samples of CSV files, sorry I can't post actual values.
Checking if a list has only numeric valuesan object meets certain criteria to amend to certain Entity Objects?
I am trying to check if a class has only numeric values in it and then return a bool
, however, some instances there may be non numeric chars in an object that should only contain numeric values.
At the present I am using reflectingreflection to loop through the object (Which in some cases can have over 100up 200 properties) and then check the property value with Regex
to see if it is a numeric value. I can't useThe reason for the regex instead of intInt.TryParse
, because sometimes even when the string is clearly only integers, or intInt64.TryParse
fails since I work with Japanese charactersis b/c every now and formatsagain a non integer value is mixed in with the text, but I know the file is an all integer file from the way it is setup. This however is not the issue as the Regex is working as expected.
Below are samples of CSV files, sorry I can't post actual values.
The string file enter image description here
The Int file enter image description here
I should also mention that the first property will always be a GUID, that also needs to be taken into consideration. (I should probably be skipping that property anyway in my current code, but obviously don't.)
I should also mention I am using a custom library to import the CSV files.
To check the value is numeric:
static bool IsMatch(string str)
{
return Regex.IsMatch(str, @"^[abcd][\d0-9]{5}$|[\d0-9]",
RegexOptions.IgnoreCase | RegexOptions.Multiline);
}
static bool IsNumericFile<T>(List<T> list)
{
foreach (var item in list)
{
List<string> values
= typeof(T).GetProperties()
.Select(propertyInfo => propertyInfo.GetValue(item, null))
.Where(s => s != null)
.Select(s => s.ToString())
.Where(str => str.Length > 0)
.ToList();
foreach (var propertyString in values)
{
var isNumeric = IsMatch(propertyString);
//Jump out if non numeric value is found
if (!isNumeric) return false;
}
}
return true;
}
Reading the file,
// Get the data from the CSV
static List<T> ImportCsv<T>(string file, string delimeter = ",", bool hasHeader = false)
{
using (var reader = File.OpenText(file))
{
var csv = new CsvReader(reader);
csv.Configuration.Delimiter = delimeter;
csv.Configuration.HasHeaderRecord = hasHeader;
csv.Configuration.MissingFieldFound = null;
csv.Configuration.HeaderValidated = null;
csv.Configuration.Encoding = Encoding.GetEncoding("Shift_JIS");
return csv.GetRecords<T>().ToList();
}
}
//Convert the data to the required entities
public IEnumerable<StringCsv> ReadStringCsvFromFile(string filePath)
{
return true;ImportCsv<GovernmentCsvRecord>(filePath).Skip(3).Select(GovernmentCsvRecord.StringCsvGovernmentToEntity);
}
public IEnumerable<IntegerCsv> ReadIntCsvFromFile(string filePath)
{
return ImportCsv<GovernmentCsvRecord>(filePath).Skip(3).Select(GovernmentCsvRecord.IntCsvGovernmentToEntity);
}
ThenI am purposefully not posting the usagecomplete GovernmentCsvRecord
object as it is an object with 200 string properties. I have posted the class with limited properties to illustrate how the class is set up with its properties and functions.
I convert to each entity via the functions in the GovernmentCsvRecord
,StringCsvGovernmentToEntity
and IntCsvGovernmentToEntity
.
The StringCsv
entity
varpublic collectionclass GovernmentCsvRecord
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
//etc 200 properties in class
public static StringCsv StringCsvGovernmentToEntity(GovernmentCsvRecord data)
{
StringCsv entity = ReadCsvFromFilenew StringCsv(file);
entity.ToListId = Guid.NewGuid();
var numericList entity.Col1 = data.Col1;
entity.Col2 = newdata.Col2;
List<bool> //etc until 200 properties are filled
}
//The `IntegerCsv` entity
public static IntegerCsv IntCsvGovernmentToEntity(GovernmentCsvRecord data);
{
foreach (var item in collection IntegerCsv entity = new IntegerCsv();
{ entity.Id = Guid.NewGuid();
//This is not what I am doing with theentity.Col1 outcome,= data.Col1;
//Just some token code when I have been runningentity.Col2 = data.Col2;
//Unitetc testsuntil 200 properties are filled
}
}
Then the usage,
List<StringCsv> StringList = new numericListList<StringCsv>();
List<IntegerCsv> IntList = new List<IntegerCsv>();
StringList = ReadStringCsvFromFile(FileName).AddToList();
if (!IsNumericFile(collectionStringList))
{
IntList = ReadIntCsvFromFile(FileName).ToList();
}
if (IntList.Count > 0)
{
//Do stuff with intList
}
else
{
//Do stuff with stringList
}
Checking if a list has only numeric values
I am trying to check if a class has only numeric values in it and then return a bool
.
At the present I am using reflecting to loop through the object (Which in some cases can have over 100 properties) and then check the property value with Regex
to see if it is a numeric value. I can't use int.TryParse
, because sometimes even when the string is clearly only integers, int.TryParse
fails since I work with Japanese characters and formats.
I should also mention that the first property will always be a GUID, that also needs to be taken into consideration. (I should probably be skipping that property anyway in my current code, but obviously don't.)
To check the value is numeric:
static bool IsMatch(string str)
{
return Regex.IsMatch(str, @"^[abcd][\d0-9]{5}$|[\d0-9]",
RegexOptions.IgnoreCase | RegexOptions.Multiline);
}
static bool IsNumericFile<T>(List<T> list)
{
foreach (var item in list)
{
List<string> values
= typeof(T).GetProperties()
.Select(propertyInfo => propertyInfo.GetValue(item, null))
.Where(s => s != null)
.Select(s => s.ToString())
.Where(str => str.Length > 0)
.ToList();
foreach (var propertyString in values)
{
var isNumeric = IsMatch(propertyString);
//Jump out if non numeric value is found
if (!isNumeric) return false;
}
}
return true;
}
Then the usage,
var collection = ReadCsvFromFile(file).ToList();
var numericList = = new List<bool>();
foreach (var item in collection)
{
//This is not what I am doing with the outcome,
//Just some token code when I have been running
//Unit tests
numericList.Add(IsNumericFile(collection));
}
Checking if an object meets certain criteria to amend to certain Entity Objects?
I am trying to check if a class has only numeric values in it and then return a bool
, however, some instances there may be non numeric chars in an object that should only contain numeric values.
At the present I am using reflection to loop through the object (Which in some cases can have up 200 properties) and then check the property value with Regex
to see if it is a numeric value. The reason for the regex instead of Int.TryParse
or Int64.TryParse
is b/c every now and again a non integer value is mixed in with the text, but I know the file is an all integer file from the way it is setup. This however is not the issue as the Regex is working as expected.
Below are samples of CSV files, sorry I can't post actual values.
The string file enter image description here
The Int file enter image description here
I should also mention that the first property will always be a GUID, that also needs to be taken into consideration. (I should probably be skipping that property anyway in my current code, but obviously don't.)
I should also mention I am using a custom library to import the CSV files.
To check the value is numeric:
static bool IsMatch(string str)
{
return Regex.IsMatch(str, @"^[abcd][\d0-9]{5}$|[\d0-9]",
RegexOptions.IgnoreCase | RegexOptions.Multiline);
}
static bool IsNumericFile<T>(List<T> list)
{
foreach (var item in list)
{
List<string> values
= typeof(T).GetProperties()
.Select(propertyInfo => propertyInfo.GetValue(item, null))
.Where(s => s != null)
.Select(s => s.ToString())
.Where(str => str.Length > 0)
.ToList();
foreach (var propertyString in values)
{
var isNumeric = IsMatch(propertyString);
//Jump out if non numeric value is found
if (!isNumeric) return false;
}
}
return true;
}
Reading the file,
// Get the data from the CSV
static List<T> ImportCsv<T>(string file, string delimeter = ",", bool hasHeader = false)
{
using (var reader = File.OpenText(file))
{
var csv = new CsvReader(reader);
csv.Configuration.Delimiter = delimeter;
csv.Configuration.HasHeaderRecord = hasHeader;
csv.Configuration.MissingFieldFound = null;
csv.Configuration.HeaderValidated = null;
csv.Configuration.Encoding = Encoding.GetEncoding("Shift_JIS");
return csv.GetRecords<T>().ToList();
}
}
//Convert the data to the required entities
public IEnumerable<StringCsv> ReadStringCsvFromFile(string filePath)
{
return ImportCsv<GovernmentCsvRecord>(filePath).Skip(3).Select(GovernmentCsvRecord.StringCsvGovernmentToEntity);
}
public IEnumerable<IntegerCsv> ReadIntCsvFromFile(string filePath)
{
return ImportCsv<GovernmentCsvRecord>(filePath).Skip(3).Select(GovernmentCsvRecord.IntCsvGovernmentToEntity);
}
I am purposefully not posting the complete GovernmentCsvRecord
object as it is an object with 200 string properties. I have posted the class with limited properties to illustrate how the class is set up with its properties and functions.
I convert to each entity via the functions in the GovernmentCsvRecord
,StringCsvGovernmentToEntity
and IntCsvGovernmentToEntity
.
The StringCsv
entity
public class GovernmentCsvRecord
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
//etc 200 properties in class
public static StringCsv StringCsvGovernmentToEntity(GovernmentCsvRecord data)
{
StringCsv entity = new StringCsv();
entity.Id = Guid.NewGuid();
entity.Col1 = data.Col1;
entity.Col2 = data.Col2;
//etc until 200 properties are filled
}
//The `IntegerCsv` entity
public static IntegerCsv IntCsvGovernmentToEntity(GovernmentCsvRecord data)
{
IntegerCsv entity = new IntegerCsv();
entity.Id = Guid.NewGuid();
entity.Col1 = data.Col1;
entity.Col2 = data.Col2;
//etc until 200 properties are filled
}
}
Then the usage,
List<StringCsv> StringList = new List<StringCsv>();
List<IntegerCsv> IntList = new List<IntegerCsv>();
StringList = ReadStringCsvFromFile(FileName).ToList();
if (!IsNumericFile(StringList))
{
IntList = ReadIntCsvFromFile(FileName).ToList();
}
if (IntList.Count > 0)
{
//Do stuff with intList
}
else
{
//Do stuff with stringList
}
- 145.5k
- 22
- 190
- 479