【问题标题】:Linq "Full Text" SearchLinq“全文”搜索
【发布时间】:2013-07-19 16:00:43
【问题描述】:

我正在使用这个搜索功能。但我需要它来做一个“和”而不是一个“或”我似乎无法让它返回我想要的结果。我需要执行搜索功能,其中搜索结果与框中输入的文本匹配。但只是部分搜索。例如,如果我输入“Super D”,我希望它找到包含“Super”和“D”的所有内容。

公共静态类 ObjectContextExtensions {

    public static IQueryable<T> FullTextSearch<T>(this IQueryable<T> queryable, string searchKey)
    {
        return FullTextSearch<T>(queryable, searchKey, false);
    }

    public static IQueryable<T> FullTextSearch<T>(this IQueryable<T> queryable, string searchKey,
                                                  bool exactMatch)
    {

        ParameterExpression parameter = Expression.Parameter(typeof(T), "c");
        MethodInfo containsMethod = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
        // MethodInfo toStringMethod = typeof (object).GetMethod("ToString", new Type[] {});


        var publicProperties =
            typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                      .Where(p => p.PropertyType == typeof(string));
        Expression orExpressions = null;
        string[] searchKeyParts;

        if (searchKey == null)
        {
            searchKey = "0";
        }
        searchKeyParts = !exactMatch ? searchKey.Split(' ') : new[] { searchKey };


        foreach (MethodCallExpression callContainsMethod in from property in publicProperties 
         select Expression.Property(parameter, property) into nameProperty 
         from searchKeyPart in searchKeyParts 

        let searchKeyExpression = Expression.Constant(searchKeyPart) let containsParamConverted = Expression.Convert(searchKeyExpression, typeof(string)) 

         select Expression.Call(nameProperty, containsMethod, (Expression)containsParamConverted))
        {
            if (orExpressions == null)
            {
                orExpressions = callContainsMethod;
            }
            else
            {
                orExpressions = Expression.Or(orExpressions,callContainsMethod);
            }
        }


        MethodCallExpression whereCallExpression = Expression.Call(
            typeof(Queryable),
            "Where",
            new Type[] { queryable.ElementType },
            queryable.Expression,
            Expression.Lambda<Func<T, bool>>(orExpressions, new ParameterExpression[] { parameter }));

        return queryable.Provider.CreateQuery<T>(whereCallExpression);
    }

}

【问题讨论】:

  • 有点乱为什么不用正则表达式?
  • 你能给我举个例子吗?我认为这种方式将是模拟全文搜索的最佳方式

标签: c# linq search full-text-search linq-expressions


【解决方案1】:

似乎将 exactMatch 设置为 true 可以解决问题,因为它不会拆分搜索词。

FullTextSearch<MyType>(searchKey, true)

如果做不到,那就改

orExpressions = Expression.Or(orExpressions,callContainsMethod);

andExpressions = Expression.And(andExpressions,callContainsMethod);

【讨论】:

  • 我尝试将 Expression.Or 更改为 Expression.And 但它不起作用。此外,如果我通过 true,它不会拆分字符串,但我需要拆分字符串并搜索“stringa”和“stringb”而不是“stringa stringb”。我希望这是有道理的
  • Expression.And 为我工作。什么对你不起作用?
  • 当我使用 Expression.And 时,我的搜索结果中没有返回任何内容。
猜你喜欢
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 2013-03-08
  • 2010-10-18
相关资源
最近更新 更多