Avoid many if-else checks. Select a table from the row using the entity structure.

I am developing an application with Entity Framework. I have a combo box with table names in the database. I have the following code:

string table = cbTables.SelectedItem.ToString(); using (var dbContext = new Entities()) { if (table.Equals("Person")) { List<Person> list = (from l in dbContext.People select l).ToList(); } else if (table.Equals("Student")) { List<Student> list = (from u in dbContext.Student select u).ToList(); } else if (table.Equals("Grade")) { List<Grade> list = (from p in dbContext.Grade select p).ToList(); } 

how can i avoid all these if-else checks? Is it possible to get the class name from a string with a name?

Example:

 string = "Person"; var str = //something List<str> list = (from u in dbContext.str select u).ToList(); 
+6
source share
4 answers

ComboBox is able to display a dictionary through a data source, so you can associate text with the data you really want, instead of checking the displayed text. Then in this case we bind it to the type of object:

 Dictionary<string,Type> typeMap = new Dictionary<string,Type> { { "Person", typeof(Person) }, { "Student", typeof(Student) }, { "Grade", typeof(Grade) }, } 

then bind it to the ComboBox as:

 cbTables.DataSource = new BindingSource(typeMap, null); cbTables.DisplayMember = "Key"; cbTables.ValueMember = "Value"; 

then when you need to get the selected object use

 Type entityType = (Type) cbTables.SelectedValue; DbSet set = dbContext.Set(entityType); 

However, after that you need to check the entityType and display the form accordingly. If your form needs a list, for example. List then use

 List<Student> studentList = set.Cast<Student>.ToList(); 
+2
source

Assuming you don't need a switch, but want something dynamic, you can use the queries:

 using (var context = new Context()) { var people = context.Database.SqlQuery<Object>( "select * from People").ToList(); } 
+1
source

Expand my comment:

You can declare a dictionary to map from the string of your table name to the actual DbSet, which is your table:

 string table = cbTables.SelectedItem.ToString(); using (var dbContext = new Entities()) { Dictionary<string, DbSet> mapping = new Dictionary<string, DbSet> { { "Person", dbContext.Person }, { "Student", dbContext.Student }, { "Grade", dbContext.Grade } }; //... 

The problem remains - what to do with your result set? Right now you have entered lists with a specific type of each table element; you cannot do this without making a decision (if / switch) of some kind.

+1
source

Why aren't you using Switch for this purpose?

 string table = cbTables.SelectedItem.ToString(); using (var dbContext = new Entities()) { switch (table) { case"Person": List<Person> list = (from l in dbContext.People select l).ToList(); break; case"Student": List<Student> list = (from u in dbContext.Student select u).ToList(); break; case"Grade": List<Grade> list = (from p in dbContext.Grade select p).ToList(); break; } } 
0
source

Source: https://habr.com/ru/post/946890/


All Articles