本文介绍了在Linq全文搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linq没有内置全文搜索功能,似乎没有关于这个主题的很多文章,所以我玩了一圈,并为我的utlity类想出了这个方法:

  public static IEnumerable< TSource> GenericFullTextSearch< TSource>(字符串文本,MyDataContext上下文)
{
//查找LINQ表属性
object [] info = typeof(TSource).GetCustomAttributes(typeof(System.Data.Linq。 Mapping.TableAttribute),true);
//获取表名
String table =(info [0] as System.Data.Linq.Mapping.TableAttribute).Name;
//在该表上进行全文搜索
return context.ExecuteQuery< TSource>(String.Concat(SELECT * FROM,table,WHERE CONTAINS(*,{0})),text );

$ / code>

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

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





$ b现在我可以用像

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

我假设目前只需进行非常基本的搜索。任何人都可以改进呢?是否有可能作为扩展方法实现并避免包装?

解决方案

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


There's no full text search built into Linq and there don't seem to be many posts on the subject so I had a play around and came up with this method for my utlity class:

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);
}

And added this wrapper to each partial Linq class where there is a full text index

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

So now I can do full text searches with neat stuff like

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

I'm assuming only a very basic search is necessary at present. Can anyone improve on this? Is it possible to implement as an extension method and avoid the wrapper?

解决方案

The neatest solution is to use an inline table valued function in sql and add it to your model

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

这篇关于在Linq全文搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 08:57