Ejemplo n.º 1
0
        public ParallelQuery <CsvMappingResult <TEntity> > Parse(IEnumerable <string> csvData)
        {
            if (csvData == null)
            {
                throw new ArgumentNullException("csvData");
            }

            var query = csvData
                        .Skip(options.SkipHeader ? 1 : 0)
                        .AsParallel();

            // If you want to get the same order as in the CSV file, this option needs to be set:
            if (options.KeepOrder)
            {
                query = query.AsOrdered();
            }

            query = query
                    .WithDegreeOfParallelism(options.DegreeOfParallelism)
                    .Where(line => !string.IsNullOrWhiteSpace(line));

            // Ignore Lines, that start with a comment character:
            if (!string.IsNullOrWhiteSpace(options.CommentCharacter))
            {
                query = query.Where(line => !line.StartsWith(options.CommentCharacter));
            }

            return(query
                   .Select(line => options.Tokenizer.Tokenize(line))
                   .Select(fields => mapping.Map(fields)));
        }
Ejemplo n.º 2
0
        public ParallelQuery <ParsedRow <TEntity> > Parse(IEnumerable <string> csvData)
        {
            if (csvData == null)
            {
                throw new ArgumentNullException("csvData");
            }

            var query = csvData
                        .Select((x, pos) => new Row(pos, x))
                        .Skip(options.SkipHeader ? 1 : 0)
                        .AsParallel();

            // If you want to get the same order as in the CSV file, this option needs to be set:
            if (options.KeepOrder)
            {
                query = query.AsOrdered();
            }

            query = query
                    .WithDegreeOfParallelism(options.DegreeOfParallelism)
                    .Where(row => !string.IsNullOrWhiteSpace(row.Data));

            // Ignore Lines, that start with a comment character:
            if (!string.IsNullOrWhiteSpace(options.CommentCharacter))
            {
                query = query.Where(row => !row.Data.StartsWith(options.CommentCharacter));
            }

            return(query
                   .Select(row => new TokenizedRow(row.Position, options.Tokenizer.Tokenize(row.Data)))
                   .Select(tokenizedRow => new ParsedRow <TEntity>(tokenizedRow.Position, mapping.Map(tokenizedRow.Data))));
        }