If I search within a description field using Linq I receive different results when searching with these two queries. First by this %MODULE%ALLEN BRADLEY%
and then second by this query %ALLEN BRADLEY%MODULE%
. I would like these search queries to give me a result where these words appear in any place of the description field.
I read about sql that using LIKE
searches for pattern in string in a correct order, for instance searching for %MODULE%ALLEN BRADLEY%
would force the result to be in that particular order within the string. However using the keyword "In" searches for the wildcards anywhere within the string.
How would I achieve that in Linq?
This is my Linq method:
public List<Item> SearchItems(string itemid, string description)
{
return (from allitems in _dbc.Items
where SqlMethods.Like(allitems.Number, itemid)
&& SqlMethods.Like(allitems.DESCRIPTION, description)
select allitems);
}
You can create your query in more then one line and then execute it using ToList()
method:
public List<Item> SearchItems(string itemid, string description)
{
IQueryable<Item> query = _dbc.Items.Where(x => SqlMethods.Like(x.Number, itemid));
foreach(var word in description.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries)
{
query = query.Where(x => SqlMethods.Like(x.Description,
string.format("%{0}%", word)));
}
return query.ToList();
}
However, as already suggested - you should consider using full text search instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.