Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CsvClassMap not found error in visual studio 2015

Tags:

csvhelper

I am trying to use CsvHelper library in one of my projects. its just console application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using System.IO;
using Newtonsoft.Json;
using CsvHelper.TypeConversion;


namespace csvtest4
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> result = new List<string>();
            string value;
            using (TextReader fileReader = File.OpenText("list.csv"))
            {
                var csv = new CsvReader(fileReader);
                csv.Configuration.HasHeaderRecord = true;
                csv.Configuration.RegisterClassMap<CustomClassMap>();
                while (csv.Read())
                {
                    for (int i = 0; csv.TryGetField<string>(i, out value); i++)
                    {
                        result.Add(value);
                    }
                }

               var json = JsonConvert.SerializeObject(result);
                Console.WriteLine(json.ToString());
            }

        }

    }

    public class CustomClassMap : CsvClassMap<TestModel>
    {
        public CustomClassMap()
        {
            Map(m => m.StringProperty).Index(0);
            Map(m => m.GuidProperty).Index(1);
            Map(m => m.IntProperty).Index(2).TypeConverter<MyCustomTypeConverter>();
        }
    }
}

Not sure, what I am missing but my vs2015 keeps complaining about CsvClassMap its saying type or namespace not found, are you missing directives. Also, it's not recognizing map function either

I think I added required namespace as other CsvHelper function working fine

like image 220
Nitin Chopra Avatar asked Jun 01 '17 17:06

Nitin Chopra


2 Answers

CsvClassMap is no longer in the latest versions of CsvHelper.

Use ClassMap<Model> instead

like image 137
Vishal Avatar answered Sep 30 '22 19:09

Vishal


CsvClassMap is in namespace CsvHelper.Configuration.

If you put your cursor on CsvClassMap and hit Ctrl+., it should pull up a menu to resolve the missing reference.

like image 30
Josh Close Avatar answered Sep 30 '22 20:09

Josh Close