Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Extension method for array to DT #147

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions FileHelpers.Tests/Tests/Common/ReadersAsDataTable.cs
Expand Up @@ -2,6 +2,8 @@
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using System.Data;


namespace FileHelpers.Tests.CommonTests
{
Expand All @@ -12,26 +14,30 @@ public class ReadersAsDataTable
public void ReadFile()
{
var engine = new FileHelperEngine<SampleType>();
var dt = engine.ReadFileAsDT(FileTest.Good.Test1.Path);
var records = engine.ReadFile(FileTest.Good.Test1.Path);

var dt = records.ToDataTable<SampleType>();

Assert.AreEqual(4, dt.Rows.Count);
Assert.AreEqual(4, engine.TotalRecords);
Assert.AreEqual(0, engine.ErrorManager.ErrorCount);

Assert.AreEqual(new DateTime(1314, 12, 11), (DateTime) dt.Rows[0]["Field1"]);
Assert.AreEqual("901", (string) dt.Rows[0]["Field2"]);
Assert.AreEqual(234, (int) dt.Rows[0]["Field3"]);
Assert.AreEqual(new DateTime(1314, 12, 11), (DateTime)dt.Rows[0]["Field1"]);
Assert.AreEqual("901", (string)dt.Rows[0]["Field2"]);
Assert.AreEqual(234, (int)dt.Rows[0]["Field3"]);

Assert.AreEqual(new DateTime(1314, 11, 10), (DateTime) dt.Rows[1]["Field1"]);
Assert.AreEqual("012", (string) dt.Rows[1]["Field2"]);
Assert.AreEqual(345, (int) dt.Rows[1]["Field3"]);
Assert.AreEqual(new DateTime(1314, 11, 10), (DateTime)dt.Rows[1]["Field1"]);
Assert.AreEqual("012", (string)dt.Rows[1]["Field2"]);
Assert.AreEqual(345, dt.Rows[1]["Field3"]);
}

[Test]
public void ReadNullableTypes()
{
var engine = new FileHelperEngine<NullableType>();
var res = engine.ReadFileAsDT(FileTest.Good.NullableTypes1.Path);
var records = engine.ReadFile(FileTest.Good.NullableTypes1.Path);

var res = records.ToDataTable<NullableType>();

Assert.AreEqual(4, res.Rows.Count);
Assert.AreEqual(4, engine.TotalRecords);
Expand Down
41 changes: 41 additions & 0 deletions FileHelpers/Core/FileHelperExtensions.cs
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace FileHelpers
{
/// <summary>
/// Set of Extension methods to be exposed to end users
/// of the FileHelper API.
/// </summary>
public static class FileHelperExtensions
{
/// <summary>
/// Generic extension method for arrays that returns the array records
/// in a DataTable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="records">The array to transform into a DataTable</param>
/// <returns>The array records in a DataTable.</returns>
public static DataTable ToDataTable<T>(this T[] records)
{
var ri = RecordInfo.Resolve(typeof(T));

return ri.Operations.RecordsToDataTable(records);
}
}
}


namespace System.Runtime.CompilerServices
{
/// <summary>
/// This is needed per http://stackoverflow.com/questions/1522605/using-extension-methods-in-net-2-0
/// This is needed because of the target framework of the project being .NET 2.0.
/// Remove this is the target framework is changed to a higher .NET version.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute { }
}

1 change: 1 addition & 0 deletions FileHelpers/FileHelpers.csproj
Expand Up @@ -172,6 +172,7 @@
<Compile Include="Converters\EnumConverter.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Core\FileHelperExtensions.cs" />
<Compile Include="Core\RecordInfo.Factory.cs" />
<Compile Include="Core\ExtractInfo.cs">
<SubType>Code</SubType>
Expand Down