c#反射表達式樹模糊搜索示例
public static Expression<Func<T, bool>> GetSearchExpression<T>(string SearchString)
{
Expression<Func<T, bool>> filter = null;
if (string.IsNullOrEmpty(SearchString)) return null;
var left = Expression.Parameter(typeof(T), "m");
Expression expression = Expression.Constant(false);
T obj = default(T);
var type = typeof(T);
obj = (T)Activator.CreateInstance(type);
var propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.Name.ToLower() == "id" || propertyInfo.PropertyType == typeof(DateTime)) continue;
Expression tostring = Expression.Call
(
Expression.Property(left, typeof(T).GetProperty(propertyInfo.Name).Name),
typeof(object).GetMethod("ToString", new Type[] { })
);
Expression right = Expression.Call
(
tostring,
typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),
Expression.Constant(SearchString)
);
expression = Expression.Or(right, expression);
}
filter = Expression.Lambda<Func<T, bool>>(expression, new[] { left });
return filter;
}
相關(guān)文章
C#正則表達式獲取下拉菜單(select)的相關(guān)屬性值
這篇文章主要介紹了C#正則表達式獲取下拉菜單(select)的相關(guān)屬性值,比如可以獲得name屬性的值、value值、指定值,需要的朋友可以參考下2014-07-07Unity3D Shader實現(xiàn)動態(tài)屏幕遮罩
這篇文章主要為大家詳細介紹了Unity3D Shader實現(xiàn)動態(tài)屏幕遮罩效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02