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
The Int file
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
}