c# - Linq Check if a string contains any query from a list -
i have list of strings search queries.
want see if string database contains of terms in query. i'd on 1 line of code, doesn't make multiple calls database. should work want more optimized.
var queries = searchquery.trim().split(' ', stringsplitoptions.removeemptyentries).distinct(); var query = context.readcontext.divisions.asqueryable(); queries.foreach(q => { query = query.where(d => (d.company.companycode + "-" + d.code).contains(q)); });
is there function can better or more optimal way of writing that?
there 2 issues proposed solution:
most linq sql providers don't understand string.contains("xyz") provider either throw exception or fetch data machine. right thing use sqlmethods.like explained in using contains() in linq sql
also, code show check whether division contains all of specified strings.
to implement 'any' behavior need construct custom expression, not possible using plain c#. need @ system.linq.expressions namespace: https://msdn.microsoft.com/en-us/library/system.linq.expressions(v=vs.110).aspx
it possible, quite involved.
Comments
Post a Comment