CsvHelper - read in multiple columns to a single list

前端 未结 4 559
梦如初夏
梦如初夏 2020-12-14 11:18

I\'m using CSVHelper to read in lots of data

I\'m wondering if it\'s possible to read the last n columns in and transpose them to a list



        
相关标签:
4条回答
  • 2020-12-14 12:04

    This does the trick as a mapper.

    public sealed class PersonMap : CsvClassMap<Person>
    {
        private List<string> attributeColumns = 
            new List<string> { "Attribute1", "Attribute2", "Attribute3" };
    
        public override void CreateMap()
        {
            Map(m => m.FirstName).Name("FirstName").Index(0);
            Map(m => m.LastName).Name("LastName").Index(1);
            Map(m => m.Attributes).ConvertUsing(row =>
                attributeColumns
                    .Select(column => row.GetField<string>(column))
                    .Where(value => String.IsNullOrWhiteSpace(value) == false)
                );
        }
    }
    

    Then you just need something like this

    using (var reader = new CsvReader(new StreamReader(filePath)))
    {
        reader.Configuration.RegisterClassMap<PersonMap>();
        while (reader.Read())
        {
            var card = reader.GetRecord<Person>();
        }
    }
    
    0 讨论(0)
  • 2020-12-14 12:05

    I don't know this library, so following might be helpful or not.

    If you already have an IEnumerable<IEnumerable<string>> which represents all records with all columns you could use this Linq query to get your List<Person> with the IList<string> Attributes:

    IEnumerable<IEnumerable<string>> allRecords = .....;
    IEnumerable<Person> allPersons = allRecords
    .Select(rec => 
    {
        var person = new Person();
        person.FirstName = rec.ElementAt(0);
        person.LastName = rec.ElementAtOrDefault(1);
        person.Attributes = rec.Skip(2).ToList();
        return person;
    }).ToList();
    

    Edit: Downloaded the library, following at least compiles, could not really test it:

    IList<Person> allPersons = new List<Person>();
    using (var reader = new CsvHelper.CsvReader(yourTextReader))
    {
        while (reader.Read())
        {
            var person = new Person();
            person.FirstName = reader.CurrentRecord.ElementAt(0);
            person.LastName = reader.CurrentRecord.ElementAtOrDefault(1);
            person.Attributes = reader.CurrentRecord.Skip(2).ToList();
            allPersons.Add(person);
        }
    }
    
    0 讨论(0)
  • 2020-12-14 12:09

    You don't actually need to define the column names beforehand, you can get them from the FieldHeaders property. So in a more dynamic version of Neil's answer you could define your Mapper as such:

    public sealed class PersonMap : CsvClassMap<Person> {
        public override void CreateMap() {
            Map(m => m.FirstName).Name("FirstName").Index(0);
            Map(m => m.LastName).Name("LastName").Index(1);
            Map(m => m.Attributes).ConvertUsing(row =>
                (row as CsvReader)?.FieldHeaders
                     .Where(header => header.StartsWith("Attribute"))
                     .Select(header => row.GetField<string>(header))
                     .Where(value => !string.IsNullOrWhiteSpace(value))
                     .ToList()
            );
        }
    }
    
    0 讨论(0)
  • 2020-12-14 12:18

    Version 3 has support for reading and writing IEnumerable properties. You can use an IList<T> property just like you have. You just need to specify the start index of the field.

    Map( m => m.Attributes ).Index( 2 );
    
    0 讨论(0)
提交回复
热议问题