I want to use dapper to query over dbf files. In my file example.dbf I have two columns:
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With