简体   繁体   中英

C#/LINQ Automated filtering on all tables

I am using C# and LINQ to SQL.

For my homework assignment i have to create an application, which will allow user to filter any table in database by some columns. The idea is that i use a DropdownList with list of tables, 3 TextBoxes + labels, DataGridView and Button. When user selects table, labels text change to match 3 first columns in selected table, then when user presses button, program performs query which will filter rows by values in textBoxes.

For example, if user chooses table "Customers", labels assigned to textBoxes change to "CustomerID", "Name", "Address". If user enters into textBoxes "10", "Smith", "11 Golden St." and presses button, DataGridView will show only users with ID=10 or Name=Smith or Address=11 Golden St.

All of this is simple, but database has 20+ tables, so it is a lot of work for just a homework. I was wondering, if there is a way to automate this process, so i didn't have to write separate code to filter every table and return results.

I already figured, that i can use myDataContext.GetType().GetProperties() and iterate through it adding property.Name to my DropdownList items to create my table list. The problem is to do the rest. Mapped table, for example Customers , doesn't have any useful properties ( Customers is of type System.Data.Linq.Table<Customer> and required properties are in class Customer ). I don't know how to extract referenced class from Linq.Table<> (ex. Customer from Linq.Table<Customer> ) and even if it is possible to build query without knowing explicitly table name (using only GetType().GetProperties() and similar).

My goal is to do (or find out if it is impossible to do) something like that:

myDropdown_SelectedIndexChanged()
{
    string tableName = myDropdown.SelectedItem.ToString();
    <some universal type> table = dataContext.GetType.GetProperty(tableName)
    .SomeFunctionIDontKnowAbout(); //something to get class "Customer" if selected table was "Customers"
    label1.Text = table.GetType().GetProperties()[0].Name; // set first column name as label's text
}

and then on button click:

string tableName = myDropdown.SelectedItem.ToString();
var query = dataContext.GetProperty(tableName)
.Select(table => table.GetProperties()[0] == textBox1.Text); 
//select from dynamically chosen table where value in first column matches text in textBox1

Is this even possible? I hope my teacher didn't give us this assignment so we would write the same code again and again for 20 different tables...

You can get the generic type of an object with Type.GetGenericArguments() like so:

Type tableType = linqTable.GetGenericArguments()[0];
label1.Text = tableType.GetProperties()[0].Name;

You might be able to create a dynamic LINQ query by building an Expression Tree , but I'm not sure how you would do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM