c# - Using SQL "In" instead of "Like" using %wildcards% in Linq -
if search within description field using linq receive different results when searching these 2 queries. first %module%allen bradley%
, second query %allen bradley%module%
. these search queries give me result these words appear in place of description field.
i read sql using like
searches pattern in string in correct order, instance searching %module%allen bradley%
force result in particular order within string. using keyword "in" searches wildcards anywhere within string.
how achieve in linq?
this linq method:
public list<item> searchitems(string itemid, string description) { return (from allitems in _dbc.items sqlmethods.like(allitems.number, itemid) && sqlmethods.like(allitems.description, description) select allitems); }
you can create query in more 1 line , execute 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, suggested - should consider using full text search instead.
Comments
Post a Comment