anyone here that can help me with my problem on our "Search" function in our application wherein they like to have the same result for this two example word which is Toy and Toys.
So for example if they searched for "Toy" then the application should return all data that has "Toy" in it or "Toys" and vise versa for "Toys".
I already use .Contains() in LINQ since this is the best way to achieve this result but when I check for "Toy" it searched for "Toy" and "Toys" and then when I searched for "Toys" it only search for "Toys" and no "Toy"
Here is my sample code:
context.CapabilityHasMethodHasSubstances
.Where(c => c.Substance.SubstanceLocale.Where(x => x.LocaleId == 2057).FirstOrDefault()
.Description.Contains(filter) && !c.IsDeleted)
.Select(c => c.CapabilityId).ToList()
Thanks in advance for someone who can help me with my problem. :)
An idea for this would be to introduce some collection of alias
for your filter
(search word).
Your task now is to resolve plural forms, which can achieved by this. Also if you have to assign synonyms to given words later, you can easily expand this.
Dictionary<string, string> AliasDict = new Dictionary<string, string>()
{
{ "Toys" , "Toy"}, // plural form
{ "Game" , "Toy"}, // synonym
{ "Games" , "Toy"}, // plural synonym
{ "Sheeps" , "Sheep" }
};
public string ResolveAlias(string alias)
{
if (AliasDict.ContainsKey(alias))
{
return AliasDict[alias];
}
return alias;
}
In your code simply at the method like this:
context.CapabilityHasMethodHasSubstances
.Where(c => c.Substance.SubstanceLocale.Where(x => x.LocaleId == 2057).FirstOrDefault()
.Description.Contains(ResolveAlias(filter)) && !c.IsDeleted)
.Select(c => c.CapabilityId).ToList()