开发者

How to build boolean combination of two MethodCallExpressions

The example uses Northwind database. The database has a "Orders" table which has "ShipName" and "ShipAddress" columns. The entity framework model in c# program has a "Order" type which has "ShipName" and "ShipAddress" properties.

I want to find the orders with Order.ShipName containing "some pattern" and Order.ShipAddress containing "other pattern". Because the ShipName and ShipAddress needs to match some pattern (can contain wild char * and ?), I have to use a stored procedure "sp_FindStringPattern" to do the string pattern search. The stored procedure returns a integer value which is mapped to System.Boolean after the stored procedure is mapped to c# function.

The follow code compiles and can run. But I don't know how to combine the two MethodCallExpression using PredicateBuilder.

    using(NorthwndEntities dataEntities = new NorthwndEntities())
    {
    // build a MethodCallExpression for Order.ShipName searching in database 
    ConstantExpression expEntity = Expression.Constant(dataEntities,typeof(NorthwndEntities));
    ParameterExpression pe = Expression.Parameter(typeof(Order), "o");
    MethodInfo minfo = dataEntities.GetType().GetMethod("sp_FindStringPattern");
   开发者_开发问答 MemberExpression me1 = Expression.PropertyOrField(pe, "ShipName");
    string strName = "A*Corp";  // "*" is wild chars
    ConstantExpression ce1 = Expression.Constant(strName, typeof(string));
    MethodCallExpression meth1 = Expression.Call(expEntity, minfo, new Expression[] { me1, ce1});
    // build a methodCallExpression for Order.ShipAddress searching in database

    MemberExpression me2 = Expression.PropertyOrField(pe, "ShipAddress");
    string strAddr = "Huntington";
    ConstantExpression ce2 = Expression.Constant(strAddr, typeof(string));
    MethodCallExpression meth2 = Expression.Call(expEntity, minfo, new Expression[] { me2, ce2});
    }

Question: How to use predicate builder to "AND" combine the two method call expression? I tried many ways, but all failed (compile and run until to build "AND" predicate). I appreciate your kind help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜