简体   繁体   English

从关键字列表中选择不同的记录

[英]Select distinct records from list of keywords

I need to generate an autocomplete sugestions list of Keywords based on a search, where each keyword has a set of KeywordSearch: 我需要根据搜索生成关键字的自动填充摘要列表,其中每个关键字都有一组KeywordSearch:

Keyword class: 关键字类别:

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

So If have a keyword like "Company Name", I will have the KeywordSearch "Company" and "Name". 因此,如果有一个像“公司名称”这样的关键字,我将使用KeywordSearch“公司”和“名称”。

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

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... 我需要基于关键字字符串构建KeywordList,因此,如果关键字=“ Compa”,则返回带有粗体样式的“ Comp”部分的“ Company Name”,或者,如果关键字=“ Compa Nam”,则返回带有带有大胆风格的“ Compa Nam”等...

Now what is happening is that it's not able to find the part "Comp" in the KeywordSearch. 现在发生的事情是,它无法在KeywordSearch中找到“比较”部分。

Any Suggestion? 有什么建议吗?

Thanks 谢谢

If I'm not mistaken w.Contains(k.Name) is the key part. 如果我没记错的话, w.Contains(k.Name)是关键部分。

w is "Compa" , k.Name is you KeywordSearch "Company" and "Name" . w"Compa"k.Name是KeywordSearch "Company""Name" So you're asking whether "Compa" contains "Company" or "Name", which is false. 因此,您要问“ Compa”是否包含“ Company”或“ Name”,这是错误的。

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. k.Name.Contains(w) (或k.Name.StartsWith(w, StringComparison.CurrentCultureIgnoreCase)如果您不希望区分大小写的话)应该返回正确的结果。

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

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