我有带有实体框架的 win 表单应用程序。在我的 DbContext 中,我有两个实例:表;列。在我的 dataAccess 层中,我有以下代码
public static class DataLoader
{
private static WdmEntities _context;
public static List<T> GetTable<T>() where T : class
{
List<T> res = new List<T>();
using (_context = new WdmEntities())
{
try
{
res = _context.Set<T>().ToList();
}
catch
{
}
}
return res;
}
}
在Form.cs
我有以下事件处理程序
availableTablesListBox.+= availableTablesListBox_SelectedIndexChanged;
void availableTablesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
//here i need to write code, that call GetTable<T> from DataLoader
//according to the SelectedIndex of availableTablesListBox
}
但我可以写(不是真正的通用)
if (availableTablesListBox.SelectedIndex == 1)
myDataGrid.DataSource = DataLoader.GetTable<tables>();
else
myDataGrid.DataSource = DataLoader.GetTable<columns>();
我想写一行代码
myDataGrid.DataSource = DataTable.GetTable<WHAT WRITE HERE>();