Example #1
0
        public List <PresidentDto> GetPresidentsInfo(PresidentFieldEnum sortByEnum, bool isDescending)
        {
            // Setup the CSV reader
            CsvHelper.Configuration.Configuration configuration = new CsvHelper.Configuration.Configuration
            {
                AllowComments            = true,
                DetectColumnCountChanges = true,
                HasHeaderRecord          = false,
                Delimiter   = ";",
                CultureInfo = CultureInfo.InvariantCulture
            };
            configuration.RegisterClassMap <PresidentMapper>();
            // Read the CSV content
            string     content    = GetCsv(Parameter.PresidentsCsv);
            TextReader textReader = new StringReader(content);
            CsvReader  csvReader  = new CsvReader(textReader, configuration);
            IEnumerable <PresidentDto> records = csvReader.GetRecords <PresidentDto>();

            // Order the list
            if (sortByEnum != PresidentFieldEnum.None)
            {
                var propertyInfo = typeof(PresidentDto).GetProperty(sortByEnum.ToString());
                if (propertyInfo != null)
                {
                    records = isDescending
                        ? records.OrderByDescending(item => propertyInfo.GetValue(item, null))
                        : records.OrderBy(item => propertyInfo.GetValue(item, null));
                }
            }

            return(records.ToList());
        }
Example #2
0
        public List <BelgianPlace> GetBelgianPlaces()
        {
            List <BelgianPlace> belgianPlaces;
            var config = new CsvHelper.Configuration.Configuration();

            config.RegisterClassMap <BelgianPlaceCsvMap>();

            StringReader sr = null;

            try
            {
                sr = new StringReader(Resources.zipcodes_alpha_beligum);
                using (var reader = new CsvReader(sr, config))
                {
                    sr            = null;
                    belgianPlaces = reader.GetRecords <BelgianPlace>().ToList();
                }
            }
            catch (Exception exn)
            {
                _logger.Error("could not parse belgian places, empty list will be used instead", exn);
                belgianPlaces = new List <BelgianPlace>();
            }
            finally
            {
                sr?.Dispose();
            }

            return(belgianPlaces);
        }
        public void Setup()
        {
            _csvConfig = new Configuration();
            _csvConfig.RegisterClassMap <CsvHelperMappingForCustomObject>();

            _fluentEngine = new DelimitedFileEngineFactory()
                            .GetEngine(new FlatFileMappingForCustomObject());

            var fixture = new Fixture();

            _records = fixture.CreateMany <CustomObject>(N).ToArray();
        }
Example #4
0
        private static void RunOutputCSV(string destinationDir, bool v, List <string> logfiles)
        {
            // Create a csv configuration objection with a custom map to enable milliseconds in the dateTime field
            CsvHelper.Configuration.Configuration csvconf = new CsvHelper.Configuration.Configuration();
            csvconf.RegisterClassMap(new LogLineMap());

            // Determine Hostname
            string host;

            if (logfiles[0].Substring(0, 1) == "\\")
            {
                // First character of log paths is \ (Remote Hostname)
                host = logfiles[0].Split('\\')[2];
            }
            else
            {
                host = System.Environment.MachineName;
            }

            if (v == false)
            {
                // Single CSV
                List <LogLine> loglines = new List <LogLine>();
                Parallel.ForEach(logfiles, (log) =>
                {
                    loglines.AddRange(ParseLogFile(log, host));
                });
                string       outfilename = destinationDir + "\\AllLogs.csv";
                StreamWriter sw          = new StreamWriter(outfilename);
                CsvWriter    csv         = new CsvWriter(sw, csvconf);
                csv.WriteRecords(loglines);
                sw.Close();
            }
            else
            {
                // Multiple CSVs
                Parallel.ForEach(logfiles, (log) =>
                {
                    List <LogLine> loglines = ParseLogFile(log, host);
                    if (loglines.Count > 0)
                    {
                        string[] filenamesplit = log.Split('\\');
                        string filename        = filenamesplit[filenamesplit.Length - 1];
                        filename           = filename.Replace(".log", ".csv");
                        string outfilename = destinationDir + "\\" + filename;
                        StreamWriter sw    = new StreamWriter(outfilename);
                        CsvWriter csv      = new CsvWriter(sw);
                        csv.WriteRecords(loglines);
                        sw.Close();
                    }
                });
            }
        }
        public void Setup()
        {
            _csvConfig = new Configuration();
            _csvConfig.RegisterClassMap <CsvHelperMappingForCustomObject>();

            _fluentEngine = new DelimitedFileEngineFactory()
                            .GetEngine(new FlatFileMappingForCustomObject());

            var records = new StringBuilder(N * 80);

            records.AppendLine("String Column,Int Column,Guid Column,Custom Type Column");
            for (int i = 0; i < N; i++)
            {
                records.AppendLine($"\"{i + 1}\",{i + 1},{Guid.NewGuid():D},{i + 1}|{i + 2}|{i + 3}");
            }

            _records = records.ToString();
        }