Skip to content

Commit

Permalink
Merge branch 'lechu445-patch-2'
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshClose committed Oct 5, 2022
2 parents e05b843 + 5ef8a88 commit 1c6e240
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/CsvHelper/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ protected virtual void ValidateHeader(ClassMap map, List<InvalidHeader> invalidH
}
else
{
var index = GetFieldIndex(parameter.Data.Names.ToArray(), parameter.Data.NameIndex, true);
var index = GetFieldIndex(parameter.Data.Names, parameter.Data.NameIndex, true);
var isValid = index != -1 || parameter.Data.IsOptional;
if (!isValid)
{
Expand Down Expand Up @@ -213,7 +213,7 @@ protected virtual void ValidateHeader(ClassMap map, List<InvalidHeader> invalidH
continue;
}

var index = GetFieldIndex(memberMap.Data.Names.ToArray(), memberMap.Data.NameIndex, true);
var index = GetFieldIndex(memberMap.Data.Names, memberMap.Data.NameIndex, true);
var isValid = index != -1 || memberMap.Data.IsOptional;
if (!isValid)
{
Expand Down Expand Up @@ -1209,7 +1209,7 @@ public virtual int GetFieldIndex(string name, int index = 0, bool isTryGet = fal
}

/// <inheritdoc/>
public virtual int GetFieldIndex(string[] names, int index = 0, bool isTryGet = false, bool isOptional = false)
public virtual int GetFieldIndex(IEnumerable<string> names, int index = 0, bool isTryGet = false, bool isOptional = false)
{
if (names == null)
{
Expand All @@ -1236,9 +1236,9 @@ public virtual int GetFieldIndex(string[] names, int index = 0, bool isTryGet =

// Check all possible names for this field.
string name = null;
for (var i = 0; i < names.Length; i++)
var i = 0;
foreach (var n in names)
{
var n = names[i];
// Get the list of indexes for this name.
var args = new PrepareHeaderForMatchArgs(n, i);
var fieldName = prepareHeaderForMatch(args);
Expand All @@ -1247,6 +1247,8 @@ public virtual int GetFieldIndex(string[] names, int index = 0, bool isTryGet =
name = fieldName;
break;
}

i++;
}

// Check if the index position exists.
Expand All @@ -1255,7 +1257,7 @@ public virtual int GetFieldIndex(string[] names, int index = 0, bool isTryGet =
// It doesn't exist. The field is missing.
if (!isTryGet && !isOptional)
{
var args = new MissingFieldFoundArgs(names, index, context);
var args = new MissingFieldFoundArgs(names.ToArray(), index, context);
missingFieldFound?.Invoke(args);
}

Expand Down
4 changes: 2 additions & 2 deletions src/CsvHelper/Expressions/ExpressionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public virtual void CreateConstructorArgumentExpressionsForMapping(ClassMap map,
if (reader.Configuration.HasHeaderRecord && (parameterMap.Data.IsNameSet || !parameterMap.Data.IsIndexSet))
{
// Use name.
index = reader.GetFieldIndex(parameterMap.Data.Names.ToArray(), parameterMap.Data.NameIndex, parameterMap.Data.IsOptional);
index = reader.GetFieldIndex(parameterMap.Data.Names, parameterMap.Data.NameIndex, parameterMap.Data.IsOptional);
if (index == -1)
{
if (parameterMap.Data.IsDefaultSet || parameterMap.Data.IsOptional)
Expand Down Expand Up @@ -228,7 +228,7 @@ public virtual void CreateMemberAssignmentsForMapping(ClassMap mapping, List<Mem
if (reader.Configuration.HasHeaderRecord && (memberMap.Data.IsNameSet || !memberMap.Data.IsIndexSet))
{
// Use the name.
index = reader.GetFieldIndex(memberMap.Data.Names.ToArray(), memberMap.Data.NameIndex, memberMap.Data.IsOptional);
index = reader.GetFieldIndex(memberMap.Data.Names, memberMap.Data.NameIndex, memberMap.Data.IsOptional);
if (index == -1)
{
if (memberMap.Data.IsDefaultSet)
Expand Down

0 comments on commit 1c6e240

Please sign in to comment.