Retreive data with simple.data from a table with its name passed in as a variable?

456 Views Asked by At

Is it possible to query a table with simple.data that has its table name passed in from somewhere else.

for example:

string tableToUse = "MyTable";
var test = db.tableToUse.All();
1

There are 1 best solutions below

0
On BEST ANSWER

Yes, you can use a string indexer for object names instead of dynamic properties:

string tableToUse = "MyTable";
var test = db[tableToUse].All();

That works for column names too, so you can do this:

var table = "MyTable";
var keyColumn = "Id";
int id = 42;
var entity = db[table].Find(db[table][keyColumn] == id);