Is this the least amount of code I can use to convert a Dapper query result to a two dimensional array?
var array =
cn.Query(@"SELECT Id, Desc FROM Things")
.Select<object, ArrayList>(d =>
{
return new ArrayList {((dynamic) d).Id, ((dynamic) d).Desc };
});
I am basically constructing a result to be returned as a json response like this:
[
[1, "Thing one"],
[2, "Thing two"],
[3, "Thing two"],
]
Serialising array
using:
string json = Newtonsoft.Json.JsonConvert.SerializeObject(array);
produces the correct result so it does the job, I'm just wondering if there's a shortcut?
Not sure about the Dapper
syntax, but what about dropping the ArrayList
, but instead returning object[]
inside the select and call ToArray()
at the end:
var array =
cn.Query(@"SELECT Id, Desc FROM Things")
.Select(d => new object[] { d.Id, d.Desc })
.ToArray();
This will give you an object[,]
.
If you dont mind a result of dynamic[,]
you can use an even shorter version, my omitting specifying the actual array type:
.Select(d => new [] { d.Id, d.Desc })