[英]How to use Local with reflection in c#?
Maybe this is a very simple thing, but I couldn't find the answer. 也许这很简单,但是我找不到答案。 Can I write the following piece of code using reflection?
我可以使用反射来编写以下代码吗?
AdminEntities context= new AdminEntities();
datagridview1.DataSource = context.TABLENAME.Local.ToBindingList();
I have tried the following: 我尝试了以下方法:
BindingSource rtBindingSource = new BindingSource();
var TableName = cboSelectTable.Text.ToString();
AdminEntities context = new AdminEntities();
var rawData = context.GetType().GetProperty(TableName).GetValue(context, null);
var truncatedData = ((IQueryable<object>)rawData).Local.ToBindingList();
var binding = new BindingList<object>(truncatedData);
rtBindingSource.DataSource = new BindingSource { DataSource = binding };
datagridview1.DataSource = rtBindingSource;
but it gives error:'System.Linq.IQueryable' does not contain a definition for 'Local' and no extension method 'Local' accepting a first argument of type 'System.Linq.IQueryable' could be found 但它给出了错误:“ System.Linq.IQueryable”不包含“ Local”的定义,并且找不到扩展方法“ Local”接受类型为“ System.Linq.IQueryable”的第一个参数
I've never done much with entity-framework
but the obvious issue looks like you're attempting to cast to the wrong object here: 我从来没有对
entity-framework
做过很多事情,但是明显的问题似乎是您试图在此处投射到错误的对象:
var truncatedData = ((IQueryable<object>)rawData).Local.ToBindingList();
I believe rawData
is a DbSet
which does not implement the IQueryable<T>
interface. 我相信
rawData
是一个DbSet
,它没有实现IQueryable<T>
接口。 Note that it does implement IQueryable
but that isn't the generic version, nor would you be able to get the Local
property from DbSet
from it anyway. 请注意,它确实实现了
IQueryable
但这不是通用版本,也无论如何都无法从DbSet
获取Local
属性。 Changing the above line to the following may help: 将上面的行更改为以下内容可能会有所帮助:
var truncatedData = ((DbSet)rawData).Local.ToBindingList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.