How to SELECT dynamic type of objects in SQLiteAsyncConnection-

1.1k Views Asked by At

I'm trying to build a database using SQLiteAsyncConnection where all of it's queries comes from the server.

database = new SQLiteAsyncConnection(dbPath);

The server's queries are divided into two parts[execution & retrieval]

for the execution queries I'm using this line of code

database.ExecuteAsync(serverResponse);

where serverResponse may be a sql CREATE, INSERT or UPDATE statement.

As for the retrieval queries I'm trying to find a way to retrieve dynamic objects from the database and serialize them to send them to the database. I'm trying to make something like this to retrieve the list of objects in database

var ls = await database.QueryAsync<dynamic>(serverResponse);

but this statement returns a List of objects, and I don't know how to use it.

I tried something like this locally

database.ExecuteAsync("CREATE TABLE Persons(PersonID int);");
database.ExecuteAsync("INSERT INTO Persons(PersonID) VALUES (5);");
var ls = await database.QueryAsync<dynamic>("SELECT * FROM Persons");
int x = ls[0].PersonID;

But it gives me this error 'object' does not contain a definition for 'PersonID'; which says that I need to cast the object to a Person class to be able to use it.(Note that I don't have any class called Person that has a public property PersonID; objects are dynamic and I can't tell what the server's query are).

I tried to serialize ls

string str = JsonConvert.SerializeObject(ls);

but it returns a JSON array with empty object [{}].

How can I serialize the result of any query without the need to cast it.

1

There are 1 best solutions below

0
On
var ls = await database.QueryAsync<object>("SELECT * FROM Persons");
var json = JsonConvert.SerializeObject(ls);

try this ?