简体   繁体   English

Linq按比赛数量排序

[英]Linq to order by number of matches

I have an array of strings, and a Linq query: 我有一个字符串数组和一个Linq查询:

/// <summary>
/// Return all the search results
/// </summary>
public static GenericTagEntity[] getSearchResults(int SectionID, string[] Words)
{
    GenericTagEntity[] Recs;

    using (MainContext db = new MainContext())
    {
        var q = (
            from c in db.tblArticles
            join a in db.tblForumAuthors on c.AuthorID equals a.Author_ID
            join u in db.tblProfiles on c.AuthorID equals u.UserID
            where c.IsDeleted == false
            && c.SectionID == SectionID
            select new
            {
                c.Title,
                c.ID,
                c.Anchor,
                c.Date,
                c.Description,
                u.EmailAddress,
                u.UserID,
                a.Username,
                c.Body,
                comments = (from f in db.tblComments where f.IsDeleted == false && f.Anchor == c.Anchor select new { f.ID }).Count()
            });

What I would like to do is modify the where so that it returns results where c.Title OR c.Body contain one or more of the words. 我想做的是修改where以便它返回结果,其中c.Titlec.Body包含一个或多个单词。 I then need it ordered by total matches (most relevant first)! 然后,我需要按总匹配数排序(最相关)。

This seems really difficult to me, any help is greatly appreciated. 这对我来说似乎真的很困难,我们将不胜感激。

I found this but it's only partially helpful, also a SO search didn't yield many results. 我发现了这一点,但这只是部分有用,SO搜索也没有产生很多结果。 http://msdn.microsoft.com/en-us/library/bb546166.aspx#Y200 http://msdn.microsoft.com/en-us/library/bb546166.aspx#Y200

Sad to break it to you but you're using wrong tool to solve this problem. 悲伤,就打破它给你,但你用错误的工具来解决这个问题。 Either create a full text index in db and invoke full text search from LINQ via a custom function or use 3rd party full text search system line Lucene. 您可以在db中创建全文索引,然后通过自定义功能从LINQ调用全文搜索,也可以使用第三方全文搜索系统行Lucene。

If you really want to do it in sql then counting no. 如果您真的想在sql中执行此操作,则不计数。 of instances of a word in a document would require you to change your db schema. 文档中单词的实例化实例将要求您更改数据库模式。

You need a words table (id, word) and a occurences table (wordid, articleid) and then you can do a query on the occurences table to get the results you want. 您需要一个单词表(id,word)和一个事件表(wordid,articleid),然后可以对事件表进行查询以获得所需的结果。

I believe this will do the trick 我相信这可以解决问题

var occurrences = from e in q.Title.Split(new char[] { ' ' }).Concat(q.Body.Split(new char[] { ' ' }))
             join word in Words on e equals word
             group e by e into g
             select new {element = g, count = g.Count()} into result
             orderby result.count descending
             select result;

The query will produce a list of words that are either in the title or body ordered descending by number of occurrences. 该查询将产生标题或正文中出现的单词列表,这些单词按出现的次数降序排列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM