Search code examples
c#simple.data

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


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();

Solution

  • 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);