c# - If statement in CompiledQueries Possible? -


basically, want execute same query different parameters based on flag. eg, following case when flag true:-

private static func<dataclassesdatacontext, int, string, iqueryable<dal.viewtype>> getfunccomponentsbylibid =         compiledquery.compile((dataclassesdatacontext dctx, int libid, string searchtext) =>             (from k in dctx.viewtypeattributes (from r in dctx.viewtypeattributes                  r.libraryid == libid &&                         r.typeattributevalue.contains(searchtext) &&                         r.typeattrstatus == null                 select r.typeid).contains(k.typeid)                 select k)); 

i want execute same query without libid parameter if flag false.

private static func<dataclassesdatacontext, int, string, iqueryable<dal.viewtype>> getfunccomponentsbylibid =         compiledquery.compile((dataclassesdatacontext dctx, int libid, string searchtext) =>             (from k in dctx.viewtypeattributes (from r in dctx.viewtypeattributes                  r.typeattributevalue.contains(searchtext) &&                         r.typeattrstatus == null                 select r.typeid).contains(k.typeid)                 select k)); 

easiest way "or"ing criteria flag.

... (!flag || r.libraryid == libid) ... 

if flag false, compared value give true.

also possible:

private static func<dataclassesdatacontext, int, string, iqueryable<dal.viewtype>> getfunccomponentsbylibid =     compiledquery.compile((dataclassesdatacontext dctx, int libid, string searchtext) => {         if(flag)             return (from k in dctx.viewtypeattributes (from r in dctx.viewtypeattributes              r.libraryid == libid &&                     r.typeattributevalue.contains(searchtext) &&                     r.typeattrstatus == null             select r.typeid).contains(k.typeid)             select k);         else             return (from k in dctx.viewtypeattributes (from r in dctx.viewtypeattributes              r.typeattributevalue.contains(searchtext) &&                     r.typeattrstatus == null             select r.typeid).contains(k.typeid)             select k);     }); 

and one:

private static func<dataclassesdatacontext, int, string, iqueryable<dal.viewtype>> getfunccomponentsbylibid =     compiledquery.compile((dataclassesdatacontext dctx, int libid, string searchtext) => {         func<viewtypeattributes, bool> wherefunc;         if(flag)             wherefunc = new func<viewtypeattributes, bool>(r => r.libraryid == libid &&                     r.typeattributevalue.contains(searchtext) &&                     r.typeattrstatus == null);         else             wherefunc = new func<viewtypeattributes, bool>(r => r.typeattributevalue.contains(searchtext) &&                     r.typeattrstatus == null);          return (from k in dctx.viewtypeattributes (from r in dctx.viewtypeattributes.where(wherefunc)             select r.typeid).contains(k.typeid)             select k);     }); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -