I have a WebApi action that returns dynamic result based on Sql query. Currently using the following function to convert IDataReader which works fine, but it creates a Dictionary object for each row. I was wondering if there is a better way to do that.
private IEnumerable ReadResult(IDataReader reader)
{
var names = new string[reader.FieldCount];
var values = new object[reader.FieldCount];
for (var i = 0; i < reader.FieldCount; i++)
{
names[i] = reader.GetName(i);
}
while (reader.Read())
{
reader.GetValues(values);
var record = names.ToDictionary(i => i, s => values[Array.IndexOf(names, s)]);
yield return record;
}
}
You can try this approach (don't exactly know how this will play together with WebAPI, but still worth a try):
var record = new ExpandoObject() as IDictionary<string, object>;
foreach(var name in names)
record[name] = values[Array.IndexOf(names, name)];
yield return record;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.