从关键字列表中选择不同的记录 [英] Select distinct records from list of keywords

查看:90
本文介绍了从关键字列表中选择不同的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要基于搜索生成关键字的自动完成摘要列表,其中每个关键字都有一组KeywordSearch:

I need to generate an autocomplete sugestions list of Keywords based on a search, where each keyword has a set of KeywordSearch:

关键字类别:

public class Keyword
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class KeywordSearch
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
    public Keyword Keyword { get; set; }
}

因此,如果有一个类似公司名称"的关键字,那么我将使用KeywordSearch公司"和名称".

So If have a keyword like "Company Name", I will have the KeywordSearch "Company" and "Name".

我现在无法正常使用的功能是:

The function I have now, that is not working well is:

public IList<KeywordDto> GetAllBySearch(string keywords, int numberOfRecords)
{
    var splitKeywords = keywords.Split(new Char[] { ' ' });
    var keywordQuery = _keywordRepository.Query.Where(p => p.IsActive == true);
    var keywordSearchQuery = _keywordSearchRepository.Query;

    var keywordIds = keywordSearchQuery
                        .GroupBy(k => k.Keyword.Id)
                        .Where(g => splitKeywords.All(w => g.Any(k => w.Contains(k.Name))))
                        .Select(g => g.Key);

    IList<KeywordDto> keywordList = (from kw in keywordQuery
                                         join kwids in keywordIds on kw.Id equals kwids
                                         select new KeywordDto { Id = kw.Id, Name = kw.Name })
                                         .Take(numberOfRecords)
                                         .Distinct()
                                         .OrderBy(p => p.Name).ToList();
    return keywordList;
}

我需要基于关键字字符串构建KeywordList,因此如果关键字="Compa",则返回带有粗体样式的"Comp"部分的"Company Name",或者,如果关键字="Compa Nam",则返回"Company名称"和"Compa Nam"等粗体样式...

I need to build a KeywordList based on the keywords string, so if keywords = "Compa" I return "Company Name" with the part "Comp" with bold style , or if keywords = "Compa Nam" I return "Company Name" with "Compa Nam" with bold style etc...

现在发生的是,它无法在KeywordSearch中找到比较"部分.

Now what is happening is that it's not able to find the part "Comp" in the KeywordSearch.

有什么建议吗?

谢谢

推荐答案

如果我没记错的话,w.Contains(k.Name)是关键部分.

If I'm not mistaken w.Contains(k.Name) is the key part.

w"Compa"k.Name是KeywordSearch "Company""Name".因此,您要问"Compa"是否包含"Company"或"Name",这是错误的.

w is "Compa", k.Name is you KeywordSearch "Company" and "Name". So you're asking whether "Compa" contains "Company" or "Name", which is false.

k.Name.Contains(w)(如果您不希望区分大小写,则为k.Name.StartsWith(w, StringComparison.CurrentCultureIgnoreCase))应该返回正确的结果.

k.Name.Contains(w) (or k.Name.StartsWith(w, StringComparison.CurrentCultureIgnoreCase) if you don't want it to be case sensitive) should return the correct result.

这篇关于从关键字列表中选择不同的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆