public static void AppendToCSV <T>(this CSVDataSet <T> csvDataSet, string targetDirectory, string targetFileName, string index, object key, Func <object, T, T> rowUpdateFunc = null) { bool fileExists = File.Exists(Path.Combine(targetDirectory, targetFileName)); using (FileWriter writer = new FileWriter(targetDirectory, targetFileName, true)) { if (fileExists == false) { writer.WriteText(csvDataSet.HeaderLine, true); } var rows = csvDataSet.GetIndexedTypedEnumerable(index, key); bool updateFuncAvailable = rowUpdateFunc != null; foreach (var row in rows) { T inRow = csvDataSet.FormatToType(row); T outRow = updateFuncAvailable ? rowUpdateFunc(key, inRow) : inRow; string csvRow = csvDataSet.FormatToCSVRow(outRow); writer.WriteText(csvRow, true); } } }
public static IEnumerable <IGrouping <TKey, TSource> > IndexGroupBy <TKey, TSource>(this CSVDataSet <TSource> csvDataSet, Func <TSource, TKey> keySelector) { IndexManager <object, Segment> workingIndexManager = new IndexManager <object, Segment>(); string indexName = "TempGroup"; var innerEnumerable = csvDataSet.GetTypedEnumerable(1); foreach (TSource row in innerEnumerable) { var key = keySelector(row); Segment segment = csvDataSet.CurrentSegment; workingIndexManager.AddMapForKey(indexName, key, segment); } IndexTable <object, Segment> indexTable = workingIndexManager.GetMap(indexName); foreach (TKey key in indexTable.GetMapKeys()) { var source = csvDataSet.GetIndexedTypedEnumerable(indexName, key, workingIndexManager); yield return(new IndexGrouping <TKey, TSource>(key, source)); } workingIndexManager = null; }