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
Post a Comment