本文介绍了实体中的动态表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想调用表名类似db.users.select(x=> x.FirstName)
的方法,但是我在某个地方犯了一个错误,我这样写,但这会在一行上返回有关表的详细信息,而不是返回我想要的表数据,请帮忙
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();
}
推荐答案
您的问题不是很清楚,但是如果您表示您想以一种通用的方式进行操作,那么是的可能的.您可以利用Set<T>
方法.例如:
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();
}
这篇关于实体中的动态表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!