dynamic table name in Entity

倖福魔咒の 提交于 2019-12-18 09:37:06

问题


I want to call method with Table name like db.users.select(x=> x.FirstName) , but i made a mistake somewhere , i write like this but this returns detail about table on one row , not return table data which i want, help please

    public void LoadToGrid(string dbTableName)
    {
      dataGridView1.DataSource = db.GetType().GetMember(dbTableName).ToList();    
    }

回答1:


Your question isn't very clear, but if you mean that You wanna do it in a generic way, then yes that is possible. You can take advantage of the Set<T> method. For example:

public void LoadToGrid<T>(GridType grid) where T: class, new() {
    grid.DataSource = dbContext.Set<T>().ToList();
}

And to customize the data fields in your grid, your can add this overload

public void LoadToGrid<T>(GridType grid, Expression<Func<T,Object>> selectExpression) where T: class, new() {
    grid.DataSource = dbContext.Set<T>().Select(selectExpression).ToList();
}


来源:https://stackoverflow.com/questions/46575714/dynamic-table-name-in-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!