2011-12-05 23 views
8

我想創建傳遞Expression<Func<T, string>類型的表達式來創建Expression<Func<T, bool>>類型的表達爲字符串屬性與StartsWithEndsWithContains過濾器的方法這樣的表述方法:創建StartsWith,的endsWith LINQ表達式和包含傳遞一個表達<Func鍵<T, string>>

.Where(e => e.MiProperty.ToUpper().StartsWith("ABC")); 
.Where(e => e.MiProperty.ToUpper().EndsWith("XYZ")); 
.Where(e => e.MiProperty.ToUpper().Contains("MNO")); 

的方法應該是這樣的:

public Expression<Func<T, bool>> AddFilterToStringProperty<T>(Expresssion<Func<T, string>> pMyExpression, string pFilter, FilterType pFiltertype) 

其中過濾式@dtb

public static Expression<Func<T, bool>> AddFilterToStringProperty<T>(
    Expression<Func<T, string>> expression, string filter, FilterType type) 
{ 
    return Expression.Lambda<Func<T, bool>>(
     Expression.Call(
      expression.Body, 
      type.ToString(), 
      null, 
      Expression.Constant(filter)), 
     expression.Parameters); 
} 
+8

去吧。讓我們知道您嘗試了什麼,如果它不起作用,我們將很樂意提供幫助。 – drdwilcox

回答

7

的枚舉類型試試這個。它工作正常,我爲這種情況下添加了一個「非空」表達式:

​​3210
+0

我已經爲此答案添加了「not null」表達式 –

4

感謝:是一個包含三個提到的操作(StartsWithEndsWithContains

相關問題