2008-11-10 32 views
45

Tôi chỉ mới bắt đầu với các cây biểu thức vì vậy tôi hy vọng điều này có ý nghĩa. Tôi cố gắng để tạo ra một cây biểu thức đại diện:Làm cách nào để tạo cây biểu thức đại diện cho 'String.Contains ("term")' trong C#?

t => t.SomeProperty.Contains("stringValue"); 

Cho đến nay tôi đã có:

private static Expression.Lambda<Func<string, bool>> GetContainsExpression<T>(string propertyName, string propertyValue) 
    { 
     var parameterExp = Expression.Parameter(typeof(T), "type"); 
     var propertyExp = Expression.Property(parameter, propertyName); 
     var containsMethodExp = Expression.*SomeMemberReferenceFunction*("Contains", propertyExp) //this is where I got lost, obviously :) 
     ... 
     return Expression.Lambda<Func<string, bool>>(containsMethodExp, parameterExp); //then something like this 
    } 

Tôi chỉ không biết làm thế nào để tham khảo các phương pháp String.Contains().

Giúp đánh giá cao.

Trả lời

109

Cái gì như:

class Foo 
{ 
    public string Bar { get; set; } 
} 
static void Main() 
{ 
    var lambda = GetExpression<Foo>("Bar", "abc"); 
    Foo foo = new Foo { Bar = "aabca" }; 
    bool test = lambda.Compile()(foo); 
} 
static Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue) 
{ 
    var parameterExp = Expression.Parameter(typeof(T), "type"); 
    var propertyExp = Expression.Property(parameterExp, propertyName); 
    MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) }); 
    var someValue = Expression.Constant(propertyValue, typeof(string)); 
    var containsMethodExp = Expression.Call(propertyExp, method, someValue); 

    return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp); 
} 

Bạn có thể thấy this hữu ích.

+0

gì nếu tôi muốn gọi không startWith "chuỗi con"? – amd

+5

@AhmD Expression.Not xung quanh Expression.Call, và thay đổi GetMethod thành StartsWith? –

+1

cảm ơn điều này chính xác những gì cần thiết! – amd

7

Làm thế nào về điều này:

Expression<Func<string, string, bool>> expFunc = (name, value) => name.Contains(value); 

Trong mã khách hàng:

bool result = expFunc.Compile()("FooBar", "Foo"); //result true 
    result = expFunc.Compile()("FooBar", "Boo");  //result false 
5

Để thực hiện tìm kiếm như:

ef.Entities.Where(entity => arr.Contains(entity.Name)).ToArray(); 

mà chuỗi dấu vết sẽ là:

SELECT .... From Entities ... Where Name In ("abc", "def", "qaz") 

tôi sử dụng phương pháp tôi đã tạo dưới đây:

ef.Entities.Where(ContainsPredicate<Entity, string>(arr, "Name")).ToArray(); 

public Expression<Func<TEntity, bool>> ContainsPredicate<TEntity, T>(T[] arr, string fieldname) where TEntity : class { 
    ParameterExpression entity = Expression.Parameter(typeof(TEntity), "entity"); 
    MemberExpression member = Expression.Property(entity, fieldname); 

    var containsMethods = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public) 
    .Where(m => m.Name == "Contains"); 
    MethodInfo method = null; 
    foreach (var m in containsMethods) { 
    if (m.GetParameters().Count() == 2) { 
     method = m; 
     break; 
    } 
    } 
    method = method.MakeGenericMethod(member.Type); 
    var exprContains = Expression.Call(method, new Expression[] { Expression.Constant(arr), member }); 
    return Expression.Lambda<Func<TEntity, bool>>(exprContains, entity); 
} 
0

Dưới đây là một mẫu của criteria.Contains (MyParam)

var method = typeof(Enumerable).GetRuntimeMethods().Single(m => m.Name == nameof(Enumerable.Contains) && m.GetParameters().Length == 2); 
var containsMethod = method.MakeGenericMethod(typeof(string)); 
var doesContain = Expression.Call(containsMethod, Expression.Constant(criteria.ToArray()), Expression.Property(p, "MyParam")); 

sử dụng thực tế tại https://raw.githubusercontent.com/xavierjohn/Its.Cqrs/e44797ef6f47424a1b145d69889bf940b5581eb8/Domain.Sql/CatchupEventFilter.cs

Các vấn đề liên quan