-
Notifications
You must be signed in to change notification settings - Fork 1
Using the AsyncFileReaders
Paul Joiner edited this page Sep 26, 2020
·
5 revisions
Beginning with version 0.3.0 of the library files may be read asynchronously using the IAsyncFileReader
interface.
💡 Note: This feature requires version 0.3.0 of the library or newer and requires dotnet standard 2.1 or dotnet Core 3.0 or above.
using DwC_A;
using DwC_A.Terms;
using System.Linq;
class Program
{
static async Task Main()
{
string fileName = "./dwca-uta_herps-v8.1.zip";
using (var archive = new ArchiveReader(fileName))
{
await foreach(var row in archive.GetAsyncCoreFile().GetDataRowsAsync())
{
//Access fields by index
Console.WriteLine(row[0]);
//Access fields by Term
Console.WriteLine(row[Terms.type]);
//Iterate over or query fields in a row using LinQ
var fields = row.Fields.ToList().Aggregate((current, next) => $"{current}\t{next}");
Console.WriteLine(fields);
}
}
}
}
Extension files may be accessed using the new GetAsyncFileReaderByFileName
or GetAsyncFileReadersByRowType
methods of the FileReaderCollection
class.
var fileName = "occurrence.txt";
IAsyncFileReader fileReader = archive.Extensions.GetAsyncFileReaderByFileName(fileName);
IEnumerable<IAsyncFileReader> fileReaders = archive.Extensions.GetAsyncFileReadersByRowType(RowTypes.Occurrence);
The IAsyncFileReader
can be used to return a ValueTask<IRow[]>
using LinQ.
var fileName = "occurrence.txt";
IAsyncFileReader fileReader = archive.Extensions.GetAsyncFileReaderByFileName(fileName);
var array = await fileReader.GetRowsAsync().ToArrayAsync();