c# - Linq Dynamic OR condition -
i having user table this,
id(int) name(string), state_id(int64), country_id(int64), mobilenumber(string), email(string), identity(string) role_id disable(boolean)
identity column have values comma separated example(,mole in hand, mole in face,). value pre-defined,
now have request this,
class userrequest { public string rolename{get;set;} public string countryname{get;set;} publi string statename{get;set;} publi ilist<string> identitylst{get;set;} }
using request want form query.
public ilist<user> getuser(userrequest req) { iqueryable<user> query=datacontext.user.where(a=>!a.isdisable) if(string.isnullorempty(req.rolename)) { query=from qu in query role in datacontext.role.where(a=>a.id==qu.role.id) role.name==req.rolename select qu; } if(string.isnullorempty(req.countryname)) { query=from qu in query country in datacontext.country.where(a=>a.id==qu.country.id) country.name==req.countryname select qu; } if(string.isnullorempty(req.statename)) { query=from qu in query state in datacontext.state.where(a=>a.id==qu.state.id) state.name==req.statename select qu; } if(req.identitylst!=null) { /////here need query or condition, possible. /////for example if identity list have 3 value. need 3 or condition } }
please me out of problem, if identitylst have 10 value hitting db 10 times.
don't qry on server post filtering @ runtime.
public ilist<user> getuser(userrequest req){ //remove [if(req.identitylst!=null)] part } public bool containsidentity(ilist<string> x, ilist<string> y) { if( x == null || y == null ) return false; return x.any(test => y.contains(test)); } var users = getuser(req).tolist(); var filtered = users.where( u => containsidentity(u.identity.split(','), req.identitylst ));
Comments
Post a Comment