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:

  1. 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

  2. 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

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -