I have two seperate calls that returns a .csv file. I need to extract a couple of headers from the files depending on which one is called. The headers does not always sit in the same index, and the CourseId
name changes to Asset_ID
in the one call.
How can I refactor the code to make it more maintainable?
Also, I think the naming is not really good.
public static class CsvHeader
{
public struct Index
{
public string Email { get; set; }
public string CompletionDate { get; set; }
public string CourseId { get; set; }
}
public static Index GetCsvHeaderIndex(string[] headers)
{
string emailIndex = null;
string completionDateIndex = null;
var email = headers.FirstOrDefault(_ => _.Contains("Email"));
var completionDate = headers.FirstOrDefault(_ => _.Contains("Completion"));
var courseId = headers.FirstOrDefault(_ => _.Contains("CourseID"));
if (!string.IsNullOrEmpty(email)) emailIndex = headers.First(_ => _.Contains("Email")).Split('(', ')')[1]; /*3*/
if (!string.IsNullOrEmpty(completionDate)) completionDateIndex = headers.First(_ => _.Contains("Completion")).Split('(', ')')[1]; /*30*/
var courseIdIndex = string.IsNullOrEmpty(courseId) ?
headers.First(_ => _.Contains("Asset_ID")).Split('(', ')')[1] :
headers.First(_ => _.Contains("CourseID")).Split('(', ')')[1];
return new Index
{
Email = emailIndex,
CompletionDate = completionDateIndex,
CourseId = courseIdIndex
};
}
}