Dapper: Not able to parse string from dbf(Error parsing column)

c# dapper dbf

Question

I want to use dapper to query over dbf files. In my file example.dbf I have two columns:

  1. Value - type NUMERIC
  2. Name - type CHARACTER

I write class ExampleDbf

class ExampleDbf
{
    public int Value { get; set; }
    public string Name { get; set; }
}

Now I want to write two simple query

var listOne = connection.Query<ExampleDbf>("SELECT value FROM Example");
var listTwo = connection.Query<ExampleDbf>("SELECT name, value FROM Example");

ListOne are OK but when I execute listTwo I Have following System.Data.DataException:

Additional information: Error parsing column 0 (name=System.Byte[] - Object)

When I use standard DataReader I must write something like that

example.name = System.Text.Encoding.ASCII.GetString((byte[])reader["name"]).Trim();

Of course I may write something like this:

class ExampleDbf2
{
    public int Value { get; set; }
    public byte[] Name { get; set; }
    public string StringName
    {
        get
        {
            return System.Text.Encoding.ASCII.GetString((byte[])Name ).Trim();
        }
    }
}

So now it works

var listTwo = connection.Query<ExampleDbf2>("SELECT name, value FROM Example");

But this solution is very ugly, maybe someone have better solution.

Accepted Answer

You could always return a dynamic, then map it to your object and perform the transformation operation during object initialization.

var listTwo = connection.Query<dynamic>("SELECT name, value FROM Example")
    .Select(x => new ExampleDbf 
        { 
            Value = x.value,  
            Name = System.Text.Encoding.ASCII.GetString((byte[])x.name).Trim()
        }).ToList();


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why