本文介绍了如何创建一个表达式树做同样的" StartsWith"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有这样的方法来比较两个数字

 专用功能ETForGreaterThan(BYVAL查询作为的IQueryable(Of T)已, BYVAL为PropertyValue作为对象,BYVAL的PropertyInfo作为的PropertyInfo)作为的IQueryable(Of T)已

尺寸E上ParameterExpression = Expression.Parameter(的GetType(T),E)
昏暗米作为MemberExpression = Expression.MakeMemberAccess(即的PropertyInfo)
尺寸C作为常量表达式= Expression.Constant(为PropertyValue,propertyValue.GetType())
尺寸b以BinaryExpression = Expression.GreaterThan(M,C)
尺寸的lambda作为一种表达(中Func键(中T,布尔))= Expression.Lambda(中Func键(中T,布尔))(b,E)
返回query.Where(拉姆达)

端功能

它工作正常,并以这种方式

$ b $消耗b

 查询= ETForGreaterThan(查询,价值的PropertyInfo)

正如你所看到的,我给它一个IQueryable收集和它where子句的属性和值添加到它,基地。 Y能够构建每种不超过,LessOrEqualThan等等同物System.Linq.Expressions.Expression有这个运营​​商预定义的。



¿我如何改造这个代码做琴弦一样吗? System.Linq.Expressions.Expression不给我一个预定义运算符,如包含或startwith,我真的表达式树的小白。



谢谢,并请在发布C#/ VB你的答案。 。选择一个让你感觉更加舒适。


解决方案

 使用系统; 
使用System.Linq的;使用System.Linq.Expressions
;
使用的System.Reflection;

命名空间WindowsFormsApplication1
{
静态类节目
{
[STAThread]
静态无效的主要()
{
使用(VAR上下文=新NorthwindEntities())
{
的PropertyInfo的PropertyInfo = typeof运算(客户).GetProperty(客户id);

&IQueryable的LT;客户>查询= context.Customers;
=查询 - ETForStartsWith LT;客户>(查询,A,的PropertyInfo);
无功名单= query.ToList();
}
}

静态的IQueryable< T> ETForStartsWith< T>(IQueryable的< T>查询字符串为PropertyValue,的PropertyInfo的PropertyInfo)
{
ParameterExpression E = Expression.Parameter(typeof运算(T),E);
MemberExpression M = Expression.MakeMemberAccess(即的PropertyInfo);
常量表达式C = Expression.Constant(为PropertyValue,typeof运算(字符串));
MethodInfo的MI = typeof运算(字符串).GetMethod(StartsWith,新类型[] {typeof运算(字符串)});
表达的呼叫= Expression.Call(男,MI,C);

表达式来; Func键< T,BOOL>>波长= Expression.Lambda<&Func键LT; T,BOOL>>(打电话,发);
返回query.Where(拉姆达);
}
}
}


Currently, I have this method to compare two numbers

Private Function ETForGreaterThan(ByVal query As IQueryable(Of T), ByVal propertyValue As Object, ByVal propertyInfo As PropertyInfo) As IQueryable(Of T)

    Dim e As ParameterExpression = Expression.Parameter(GetType(T), "e")
    Dim m As MemberExpression = Expression.MakeMemberAccess(e, propertyInfo)
    Dim c As ConstantExpression = Expression.Constant(propertyValue, propertyValue.GetType())
    Dim b As BinaryExpression = Expression.GreaterThan(m, c)
    Dim lambda As Expression(Of Func(Of T, Boolean)) = Expression.Lambda(Of Func(Of T, Boolean))(b, e)
    Return query.Where(lambda)

End Function

It works fine and is consumed in this way

query = ETForGreaterThan(query, Value, propertyInfo)

As you can see, I give it an IQueryable collection and it add a where clause to it, base on a property and a value. Y can construct Lessthan, LessOrEqualThan etc equivalents as System.Linq.Expressions.Expression has this operators predefined.

¿How can I transform this code to do the same with strings? System.Linq.Expressions.Expression don't give me a predefined operator like "contains" or "startwith" and I'm really noob with Expression trees.

Thanks, and please Post your answer in C#/VB. Choose the one that make you feel more confortable.

解决方案
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            using (var context = new NorthwindEntities())
            {
                PropertyInfo propertyInfo = typeof(Customer).GetProperty("CustomerID"); 

                IQueryable<Customer> query = context.Customers;
                query = ETForStartsWith<Customer>(query, "A", propertyInfo); 
                var list = query.ToList();
            }
        }

        static IQueryable<T> ETForStartsWith<T>(IQueryable<T> query, string propertyValue, PropertyInfo propertyInfo)
        {
            ParameterExpression e = Expression.Parameter(typeof(T), "e");
            MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo);
            ConstantExpression c = Expression.Constant(propertyValue, typeof(string));
            MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
            Expression call = Expression.Call(m, mi, c);

            Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(call, e); 
            return query.Where(lambda);
        }
    }
}

这篇关于如何创建一个表达式树做同样的&QUOT; StartsWith&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 05:11