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
Dappersyntax, but what about dropping theArrayList, but instead returningobject[]inside the select and callToArray()at the end: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: