【问题标题】:C# Dynamic Expression Tree for search list using Where (or Any) and generics使用 Where(或 Any)和泛型的搜索列表的 C# 动态表达式树
【发布时间】:2017-04-18 12:10:13
【问题描述】:

目前,我有一个有效的辅助函数,它创建一个简单的表达式来搜索通用实体 (TEntity) 中的搜索词(单个单词/短语)。它创建以下表达式:

q => q.Product.ProductTitle.Contains( searchText: 'red shirt' )

我只需要扩展这个助手来搜索搜索词中包含的每个单词(例如 searchText:'red shirt' --> searchTerms:['red','shirt'])

var searchTerms = searchText.ToLower().Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries ).ToList();
searchTerms.Any( s => !q.Product.ProductTitle.ToLower().Contains( s ) )

功能如下——我已经注释了需要完成的代码。

    public static Expression<Func<TEntity, bool>> CreateSearchQuery( List<PropertyInfo> searchPropertiesList, string searchText, SearchType searchType = SearchType.Contains )
    {
        if( string.IsNullOrWhiteSpace( searchText ) || searchPropertiesList == null || searchPropertiesList.Count <= 0 )
        {
            return null;
        }

        var searchTerms = searchText.ToLower().Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries ).ToList();
        var searchExpressionList = new List<Expression<Func<TEntity, bool>>>();
        foreach( var property in searchPropertiesList.Where( x => x.GetCustomAttributes( false ).Any( c => c.GetType() != typeof( NotMappedAttribute ) ) ) )
        {
            //search param
            var searchParam = Expression.Parameter( typeof( string ), "s" );

            //search type
            var searchTypeMethod = typeof( string ).GetMethod( searchType.ToString(), new[] { typeof( string ) } );

            //entity expression.
            var entityParam = Expression.Parameter( typeof( TEntity ), "q" );
            var entityProperty = Expression.Property( entityParam, property );
            var entityExpression = Expression.Call(
                Expression.Call( entityProperty, typeof( string ).GetMethod( "ToLower", System.Type.EmptyTypes ) ),
                searchTypeMethod,
                Expression.Call( searchParam, typeof( string ).GetMethod( "ToLower", System.Type.EmptyTypes ) )
            );
            var entityPredicateBody = Expression.Lambda<Func<TEntity, bool>>( entityExpression, entityParam );

            ////TODO: CONSIDER EACH TERM AND CREATE WHERE/ANY EXPRESSION
            //searchTerms.Any( s => !q.Product.ProductTitle.ToLower().Contains( s ) )
            //var filterExpression = Expression.Call(
            //    typeof( Enumerable ),
            //    "Where",
            //    new[] { typeof( TEntity ) },
            //    searchParam,
            //    entityPredicateBody );
            //var expressionBody = Expression.Lambda<Func<TEntity, bool>>( filterExpression, searchParam );

            //TODO: REPLACE WITH NEW EXPRESSION (expressionBody)
            searchExpressionList.Add( entityPredicateBody );
        }

【问题讨论】:

  • 为什么你有一个用单引号('red shirt')包裹的字符串?如果您希望能够扩展它以接受多个字符串,您可以将其用作您的函数 args:(List&lt;PropertyInfo&gt; searchPropertiesList, SearchType searchType, params string[] args),然后您只需要处理传入的 1 个(或更多)args。请参阅:msdn.microsoft.com/en-us/library/w5zay9db.aspx
  • 感谢您的反馈扎克。我会考虑在未来更新或改变行为。

标签: c# linq generics lambda expression


【解决方案1】:

应该是:

public static Expression<Func<TEntity, bool>> CreateSearchQuery<TEntity>(List<PropertyInfo> properties, string text, SearchType searchType = SearchType.Contains)
{
    if (string.IsNullOrWhiteSpace(text) || properties == null || properties.Count == 0)
    {
        return null;
    }

    // For comparison
    //Expression<Func<ProductContainer, bool>> exp = q => searchText.Any(x => q.Product.ProductTitle.ToLower().Contains(x));

    var expressions = new List<Expression>();

    var entity = Expression.Parameter(typeof(TEntity), "q");

    //search type
    var searchMethod = typeof(string).GetMethod(searchType.ToString(), new[] { typeof(string) });

    //search terms
    var searchTerms = Expression.Constant(text.ToLower().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

    //search param
    var str = Expression.Parameter(typeof(string), "s");

    foreach (var property in properties.Where(
        x => x.GetCustomAttribute<NotMappedAttribute>() == null))
    {
        var entityProperty = Expression.Property(entity, property);
        var toLower = Expression.Call(entityProperty, "ToLower", Type.EmptyTypes);
        var contains = Expression.Call(toLower, searchMethod, str);

        var anyExpression = Expression.Lambda<Func<string, bool>>(contains, str);

        var any = Expression.Call(typeof(Enumerable), "Any", new[] { typeof(string) }, searchTerms, anyExpression);

        expressions.Add(any);
    }

    var ors = expressions.Aggregate((x, y) => Expression.Or(x, y));

    var exp = Expression.Lambda<Func<TEntity, bool>>(ors, entity);
    return exp;
}

我已将各种属性生成的所有各种表达式放入or(方法末尾附近的Aggregate)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多