there.
I’ve got a database that contains 3 tables: Animals, Birds and Fishes.
For using them in my code I write myDb variable as DbContext with DbSet objects.
So, I want to clean up code from this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "Animals":
myDb.Animals.Load();
dataGridView1.DataSource = myDb.Animals.Local.ToBindingList();
break;
case "Birds":
myDb.Birds.Load();
dataGridView1.DataSource = myDb.Birds.Local.ToBindingList();
break;
case "Fishes":
myDb.Fishes.Load();
dataGridView1.DataSource = myDb.Fishes.Local.ToBindingList();
break;
}
}
To this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
[some type] table;
switch (comboBox1.Text)
{
case "Animals":
table = myDb.Animals;
break;
case "Birds":
table myDb.Birds;
break;
case "Fishes":
table = myDb.Fishes;
break;
}
table.Load();
dataGridView1.DataSource = table.Local.ToBindingList();
}
Is there way to make it?
I’ve tried var table as object, but i can’t call DbSet methods.
New contributor
Dimkqo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.