如何使用Express.call方法创建(谓词)从DataTable中获取数据 [英] How to create (predicate) for fetching data from DataTable using Express.call method

查看:65
本文介绍了如何使用Express.call方法创建(谓词)从DataTable中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Home Controller中有一个数据表,如下所示:

I have a DataTable in Home Controller as follows:

public DataTable GetTable()
{
    DataTable table = new DataTable();
    table.Columns.Add("Dosage", typeof(int));
    table.Columns.Add("Drug", typeof(string));
    table.Columns.Add("Patient", typeof(Info));
    table.Columns.Add("Date", typeof(DateTime));

    table.Rows.Add(25, "Indocin", new Info("India"), DateTime.Now);
    table.Rows.Add(50, "Enebrel", new Info("UK"), DateTime.Now);
    table.Rows.Add(10, "Hydralazine", new Info("Bhutan"), DateTime.Now);
    table.Rows.Add(21, "Combivent", new Info("India"), DateTime.Now);
    table.Rows.Add(100, "Dilantin", new Info("GreenLand"), DateTime.Now);
    return table;
}

信息类如下

public class Info
{
    public string Address { get; set; }
    public Info(string Add) {
        this.Address = Add;
    }
}

现在,我想基于地址字段(即Patient.Address)进行过滤操作

Now, i want to do the filtering operation based on Address Field, i.e Patient.Address

在这里,患者是Info类的对象

Here, Patient is object of Info class

我需要形成条件以获取数据.

I need to form the condition, in order to fetch the data.

我正在使用Express.call方法形成条件

I am forming condition using Express.call method

private static MethodCallExpression GetFieldCallExpression(Expression memExp, Type ColumnType, string ColumnName)
        {
            MethodInfo method = typeof(DataRowExtensions).GetMethods().Where(m => m.Name == "Field" && m.IsGenericMethod && m.GetParameters().Count() == 2 && m.GetParameters()[1].ParameterType == typeof(string)).FirstOrDefault();
            var genericWrapper = method.MakeGenericMethod(new Type[] { ColumnType });
            var toLowerMethodCall = Expression.Call(
                 null,
                 genericWrapper,
                 memExp,
                 Expression.Constant(ColumnName, ColumnName.GetType())
                 );
            return toLowerMethodCall;
        }

在这里,memExp-DataRow的实例 columnName-Patient.Address columnType-字符串

Here, memExp - instance of DataRow columnName - Patient.Address columnType - string

谓词是这样形成的

收到消息,

列'Patient.Address'不属于表."

"Column 'Patient.Address' does not belong to table ."

我在哪里犯错了

推荐答案

DataRow值访问器仅支持顶级"列名称(例如示例中的剂量",药物",患者"等). .为了实现您的目标,您必须拆分路径并手动生成访问器.

DataRow value accessors support only the "top level" column names (like "Dosage", "Drug", "Patient" etc. in your sample). In order to achieve your goal, you have to split the path and generate accessors manually.

例如,"Patient.Address"的示例访问器应为dr.Field<Info>("Patient").Address或换句话说:

For instance, the sample accessor for "Patient.Address" should be dr.Field<Info>("Patient").Address or in other words:

var value1 = dr.Field<Info>("Patient");
var value2 = value1.Address;
...
return value2;

以下是通用实现的方法:

Here is the same implemented generically:

private static Expression GetFieldValueExpression(Expression source, Type columnType, string memberPath)
{
    var memberNames = memberPath.Split('.');
    Expression value = Expression.Call(
        typeof(DataRowExtensions), "Field", new Type[] { columnType },
        source, Expression.Constant(memberNames[0]));
    for (int i = 1; i < memberNames.Length; i++)
        value = Expression.PropertyOrField(value, memberNames[i]);
    return value;
}

这篇关于如何使用Express.call方法创建(谓词)从DataTable中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆