1

I need to generate an autocomplete sugestions list of Keywords based on a search, where each keyword has a set of 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".

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...

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

Any Suggestion?

Thanks

Xaruth
  • 4,034
  • 3
  • 19
  • 26
Patrick
  • 2,995
  • 14
  • 64
  • 125

1 Answers1

1

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

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) (or k.Name.StartsWith(w, StringComparison.CurrentCultureIgnoreCase) if you don't want it to be case sensitive) should return the correct result.

Kabbalah
  • 471
  • 1
  • 5
  • 16
  • Hi, thanks it's working great! ;) BTW, can you take a look at this one: http://stackoverflow.com/q/19796132/1480877 it's similar but a bit more complex. Thanks – Patrick Nov 05 '13 at 18:32