c# - Linq multi keyword search -
i have situation have keyword column in table contains comma separated keywords. user can provide multiple search terms search against keyword column. example user provides below search terms "one,two,three" , db column may contain of "one" ,"two", "three" or none of them. how can write query matches against of provided search term.
i have tried below
string[] searchterm = {"one","two","three"}; query =product.where( p=> searchterm.any(val => p.keywords.contains(val)));
and
var query=db.products; foreach (string searchword in searchterm) { query = query.where(c => c.keywords.contains(searchword )); }
but not working me.
thanks,
if split text provided user in different keywords, in converting "one,two",three" in string[], like:
string [] keys = keywords.split(','); var query = new list<product>(); foreach (var item in keys) { query = query.concat ((ienumerable<product>)product.where(x=>x.keywords.contains(item))).tolist(); }
example:
list<product> ass = new list<product>(); product = new product(); a.keywords = "one, two, three"; product a1 = new product(); a1.keywords = "one, three"; product a2 = new product(); a2.keywords = "five"; ass.add(a); ass.add(a1); ass.add(a2); string userinput = "one, seven"; string [] keys = userinput.split(','); var query = new list<product>(); foreach (var item in keys) { query = query.concat ((ienumerable<product>)ass.where(x=>x.keywords.contains(item))).tolist(); } foreach (var item in query ) { console.writeline(item.keywords); }
prints:
one, two, 3 one, 3
Comments
Post a Comment