I am trying to use a string to determine the table that I am performing my query on. However I can not find any way to do this. Here is my code:
ADVENTUREWORKSSUPEREntities context = new ADVENTUREWORKSSUPEREntities();
string table = "Addresses" //this is set elsewhere in the code but I put it here for clarity
if (table == "Addresses")
{
results.ItemsSource = context.Addresses.Where(condition).ToList();
}
else if (table == "Customers")
{
results.ItemsSource = context.Customers.Where(condition).ToList();
}
...
...
...
else if (table == "SalesOrderHeaders")
{
results.ItemsSource = context.SalesOrderHeaders.Where(condition).ToList();
}
Is it possible to replace the
results.ItemsSource = context.Addresses.Where(condition).ToList();
with a line that uses my table string instead of Addresses?
Thus it would be
results.ItemsSource = context.[table].Where(condition).ToList();
Edit:
I’m doing this as an exercise to learn wpf/entity framework/C#. As I am watching videos on puralsight I am thinking of stuff to try and adding it to this program.
Here is the solution that I came up with.
Thanks to everyone that commented/answered for all the help and pointing me in good directions.