Skip to content

Commit 614cdd2

Browse files
committed
Merge branch 'tkestowicz-master' into master.
2 parents 4cd1f6a + 2359852 commit 614cdd2

8 files changed

Lines changed: 37 additions & 9 deletions

src/LinqToExcel.Tests/ColumnMappings_IntegrationTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using NUnit.Framework;
44
using System.IO;
55
using log4net.Core;
6-
using System.Data.OleDb;
76

87
namespace LinqToExcel.Tests
98
{
@@ -139,5 +138,19 @@ public void annotated_properties_map_to_columns()
139138
Assert.AreEqual(new DateTime(1988, 7, 26), rival.StartDate, "StartDate");
140139
Assert.AreEqual("N", rival.IsActive, "IsActive");
141140
}
141+
142+
[Test]
143+
public void two_transformations_with_the_same_property_but_in_different_types()
144+
{
145+
_repo.AddTransformation<Transformation1>(x => x.Value, x => x);
146+
_repo.AddTransformation<Transformation2>(x => x.Value, x => x);
147+
148+
var value1 = _repo.Worksheet<Transformation1>("Transformation1").First().Value;
149+
var value2 = _repo.Worksheet<Transformation2>("Transformation2").First().Value;
150+
151+
Assert.AreEqual(1, value1);
152+
Assert.AreEqual("some different value", value2);
153+
}
154+
142155
}
143156
}

src/LinqToExcel.Tests/ConfiguredWorksheetName_IntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void worksheetIndex_of_2_uses_third_table_name_orderedby_name()
5050
[Test]
5151
public void worksheetIndex_too_high_throws_exception()
5252
{
53-
Assert.That(() => from c in ExcelQueryFactory.Worksheet<Company>(8, _excelFileName, new LogManagerFactory())
53+
Assert.That(() => from c in ExcelQueryFactory.Worksheet<Company>(100, _excelFileName, new LogManagerFactory())
5454
select c,
5555
Throws.TypeOf<DataException>());
5656
}
1.5 KB
Binary file not shown.

src/LinqToExcel.Tests/ExcelQueryFactoryTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ public void GetWorksheetNames_returns_worksheet_names()
8080

8181
var worksheetNames = excel.GetWorksheetNames();
8282
Assert.AreEqual(
83-
"ColumnMappings, IMEX Table, Invalid Cast, More Companies, Null Dates, Range1, Sheet1, TrimSpaces",
84-
string.Join(", ", worksheetNames.ToArray()));
83+
"ColumnMappings, IMEX Table, Invalid Cast, More Companies, Null Dates, Range1, Sheet1, Transformation1, Transformation2, TrimSpaces",
84+
string.Join(", ", worksheetNames.ToArray())
85+
);
8586
}
8687

8788
[Test]

src/LinqToExcel.Tests/LinqToExcel.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
<Compile Include="Row_IntegrationTests.cs" />
120120
<Compile Include="Row_SQLStatement_UnitTests.cs" />
121121
<Compile Include="SQLLogStatements_Helper.cs" />
122+
<Compile Include="Transformations.cs" />
122123
<Compile Include="UnSupportedMethods.cs" />
123124
<Compile Include="WorkSheetNameTests.cs" />
124125
</ItemGroup>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace LinqToExcel.Tests
2+
{
3+
public class Transformation1
4+
{
5+
public int Value { get; set; }
6+
}
7+
8+
public class Transformation2
9+
{
10+
public string Value { get; set; }
11+
}
12+
}

src/LinqToExcel/ExcelQueryFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private string GetPropertyName<TSheetData>(Expression<Func<TSheetData, object>>
137137
/// </example>
138138
public void AddTransformation<TSheetData>(Expression<Func<TSheetData, object>> property, Func<string, object> transformation)
139139
{
140-
_transformations.Add(GetPropertyName(property), transformation);
140+
_transformations.Add(string.Format("{0}.{1}", typeof(TSheetData).Name, GetPropertyName(property)), transformation);
141141
}
142142

143143
/// <summary>

src/LinqToExcel/Query/ExcelQueryExecutor.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ private IEnumerable<object> GetTypeResults(IDataReader data, IEnumerable<string>
331331
{
332332
if (columns.Contains(columnName))
333333
{
334-
var value = GetColumnValue(data, columnName, prop.Name).Cast(prop.PropertyType);
334+
var value = GetColumnValue(data, columnName, prop.Name, fromType.Name).Cast(prop.PropertyType);
335335
value = TrimStringValue(value);
336336
result.SetProperty(prop.Name, value);
337337
}
@@ -402,11 +402,12 @@ private bool ColumnIsNotMapped(string columnName)
402402
return !_args.ColumnMappings.Values.Contains(columnName);
403403
}
404404

405-
private object GetColumnValue(IDataRecord data, string columnName, string propertyName)
405+
private object GetColumnValue(IDataRecord data, string columnName, string propertyName, string typeName)
406406
{
407+
var transformationKey = string.Format("{0}.{1}", typeName, propertyName);
407408
//Perform the property transformation if there is one
408-
return (_args.Transformations.ContainsKey(propertyName)) ?
409-
_args.Transformations[propertyName](data[columnName].ToString()) :
409+
return (_args.Transformations.ContainsKey(transformationKey)) ?
410+
_args.Transformations[transformationKey](data[columnName].ToString()) :
410411
data[columnName];
411412
}
412413

0 commit comments

Comments
 (0)