简体   繁体   English

实体框架反射调用表的where方法,MVC C#

[英]Entity Framework reflection invoke where method of a table, mvc c#

Trying to do a reflection call to table and invoke the where method. 尝试对表进行反射调用并调用where方法。 Use in MVC (2), 在MVC(2)中使用,

Data in entity framework. 实体框架中的数据。 Could hardcode for the currently two tables that im trying to get data out off. 可以为当前试图删除数据的两个表进行硬编码。

Trouble at how to invoke the where method. 如何调用where方法很麻烦。

And simply surprised that I did not find answer to this already? 只是感到惊讶,我还没有找到答案? So if already answered, and iv not searched the right thing, im sure you'll let me know. 因此,如果已经回答了,并且iv没有搜索正确的内容,我想您会让我知道。

have the following 有以下

string search_table

PropertyInfo prop = database.GetType().GetProperty(search_table);
object table = prop.GetValue(WRG,null);
var methods = table.GetType().GetMethods();

var where_method = table.GetType().GetMethod("Where");
var result_data = where_method.Invoke(table, new object[]{"table", "table.-FIELD-NAME.Contains(search_string)"});

If not using reflection, then I would call the where method with var result_data = database.-TABEL-.Where(t => t.-FIELD.Contains(search_string)); 如果不使用反射,那么我将使用var result_data = database.-TABEL-.Where(t => t.-FIELD.Contains(search_string));调用where方法。

So have tried copping that info into the invoke, nothing. 因此,尝试将该信息复制到调用中,什么也没有。

where invoke asks for: (object obj, object[] parameters) 在哪里调用要求:(对象obj,object []参数)

thank you anyone pointing me in the right direction. 谢谢任何人指出我正确的方向。

Where is an extension method , usually defined in the System.Linq.Queryable or System.Linq.Enumerable classes, depending on whether or not the object implements IQueryable . 扩展方法 Where ,通常在System.Linq.QueryableSystem.Linq.Enumerable类中定义,具体取决于对象是否实现IQueryable

The Enumerable version expects a Func<T, bool> parameter. Enumerable版本需要一个Func<T, bool>参数。 The Queryable version expects an Expression<Func<T, bool>> parameter. Queryable的版本期望的Expression<Func<T, bool>>参数。 Neither version accepts a string representation of the parameter. 两种版本均不接受参数的字符串表示形式。 If you need to pass the parameter as a string, you'll need to look at the LINQ Dynamic Query library . 如果需要将参数作为字符串传递,则需要查看LINQ动态查询库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM