加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

全文检索Linq

发布时间:2020-12-14 22:21:42 所属栏目:资源 来源:网络整理
导读:Linq没有内置的全文搜索,似乎没有很多关于这个问题的帖子,所以我玩了一个玩,并提出了这个方法为我的utlity类: public static IEnumerableTSource GenericFullTextSearchTSource(string text,MyDataContext context){ //Find LINQ Table attribute object
Linq没有内置的全文搜索,似乎没有很多关于这个问题的帖子,所以我玩了一个玩,并提出了这个方法为我的utlity类:
public static IEnumerable<TSource> GenericFullTextSearch<TSource>(string text,MyDataContext context)
{
    //Find LINQ Table attribute
    object[] info = typeof(TSource).GetCustomAttributes(typeof(System.Data.Linq.Mapping.TableAttribute),true);
    //Get table name
    String table = (info[0] as System.Data.Linq.Mapping.TableAttribute).Name;
    //Full text search on that table
    return context.ExecuteQuery<TSource>(String.Concat("SELECT * FROM ",table," WHERE CONTAINS(*,{0})"),text);
}

并将这个包装器添加到每个部分Linq类,其中有一个全文索引

public static IEnumerable<Pet> FullTextSearch(string text,MyDataContext context)
{
    return (LinqUtilities.GenericFullTextSearch<Pet>(text,context) as IEnumerable<Pet>);
}

所以现在我可以用整齐的东西做全文搜索

var Pets = Pet.FullTextSearch(helloimatextbox.Text,MyDataContext).Skip(10).Take(10);

我假设目前只需要非常基本的搜索。任何人都可以改进吗?是否可以实现一个扩展方法并避免包装?

解决方法

最简单的解决方案是在sql中使用内联表值函数,并将其添加到模型中

http://sqlblogcasts.com/blogs/simons/archive/2008/12/18/LINQ-to-SQL—Enabling-Fulltext-searching.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读