获取表名LINQtoSQL

时间:2014-08-29 18:29:54

标签: c# .net linq-to-sql datacontext

我的组合框中包含了我数据库中所有表的列表:

using(DataContext db = new DataContext())
{
    cmbbx_tables.DataSource = db.Mapping.GetTables().Select(q=>new {q.TableName, q.RowType}).ToArray();
    cmbbx_tables.DisplayMember = "TableName";
    cmbbx_tables.ValueMember = "RowType";        
}

SelectionChangeCommitted事件中,我想获取所选表的名称:

private void cmbbx_tables_SelectionChangeCommitted(object sender, EventArgs e)
{
    using(DataContext db = new DataContext())
    {
        String table = db.Mapping.GetTable((???)cmbbx_tables.SelectedValue).TableName;                
    }
}

我应该如何输入cast cmbbx_tables.SelectedValue?

1 个答案:

答案 0 :(得分:0)

解决方案是:

String name = db.Mapping.GetTable((cmbbx_tables.SelectedValue as System.Data.Linq.Mapping.MetaType).Type).TableName;