Skip to content

Instantly share code, notes, and snippets.

@SebastianMartens
Created October 31, 2013 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SebastianMartens/7250969 to your computer and use it in GitHub Desktop.
Save SebastianMartens/7250969 to your computer and use it in GitHub Desktop.
Cool snippet to quickly import data in any desired format from csv files...
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace myGists
{
/// <summary>
/// Load data from csv files in a simple, elegant way...
/// </summary>
public class CSVLoader
{
#region generic File reading methods
/// <summary>
/// Converts a CSV-File to any desired Format. The File is read line by line
/// and splitted with the separator
/// </summary>
/// <typeparam name="TTarget">The type of the target.</typeparam>
/// <param name="fullFileName">The filename.</param>
/// <param name="projection">The projection.</param>
/// <returns></returns>
private static IEnumerable<TTarget> ConvertCsvTo<TTarget>(string fullFileName, Func<string[], TTarget> projection)
{
return ConvertCsvTo(
fullFileName,
'|', // use our default seperator.
rowIndex => rowIndex > 0, // do NOT import header row as data column!
projection,
Encoding.UTF7);
}
/// <summary>
/// Converts a CSV-File to any desired Format. The File is read line by line
/// and splitted with the separator
/// </summary>
/// <typeparam name="TTarget">The type of the target.</typeparam>
/// <param name="fullFileName">The filename.</param>
/// <param name="separator">The separator.</param>
/// <param name="predicateRowCount">The predicate determining wether a row should be parsed.
/// The predicate expression is evaluated based on the current row index.</param>
/// <param name="projection">The projection.</param>
/// <param name="encodingType">Type of the encoding.</param>
/// <returns></returns>
private static IEnumerable<TTarget> ConvertCsvTo<TTarget>(
string fullFileName,
char separator,
Predicate<int> predicateRowCount,
Func<string[], TTarget> projection,
Encoding encodingType)
{
if (!File.Exists(fullFileName))
{
// TODO: Multilanguage
throw new FileNotFoundException(String.Format("Die angegebene Datei konnte nicht gefunden werden: {0}", fullFileName), fullFileName);
}
var rowCount = 0;
return from row in File.ReadAllLines(fullFileName, encodingType)
where predicateRowCount(rowCount++)
let columns = row.Split(separator)
select projection(columns);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment