c# - Using a Dictionary with PredicateBuilder? -
i have dictionary<string,object>
of search terms , values
. in case term
key
. example is:
var args = new dictionary<string,object>() {{"name","joe"}, {"occupation","salesman"}};
i using predicatebuilder
build where
clause , works, wondering if there more concise way dictionary
instead of having call if(args.containskey(...)
every search term
in dictionary
? how doing now:
list<person> persons; var predicate = predicatebuilder.false<person>(); if (args.containskey("name")) { string name = args["name"].tostring(); predicate = predicate.or(x => x.name == name); } if (args.containskey("name")) { string age = args["age"].tostring(); predicate = predicate.or(x => x.age == age); } persons = _context.persons.asexpandable().where(predicate).tolist();
also, if declare string name
outside if block
, resharper
says access modified closure
, matter in case? example
string name; if (args.containskey("name")) { name = args["name"].tostring(); predicate = predicate.or(x => x.name == name); //access modified closure }
but in above, not modifying name
anywhere, why tell me this?
i new predicatebuilder, if explain predicatebuilder.false<person>()
means opposed predicatebuilder.true<person>()
, compile
method do?
also, if declare string name outside if block, resharper says access modified closure, matter in case?
the code still work despite fact code generates warning. happens not problem unless modify name
variable time before invoke closure. in code you've shown never use again, wouldn't problem, if ever modified in code shown after code have problem.
why want in first place? starters, don't see point of variable begin with. why not write: x => x.name == args["name"]
? if did want local variable this, why want scope @ higher level? should scope variable @ lowest scope possible while still allowing accessed needs be. shouldn't modifying outside if
, don't expose outside if
.
i new predicatebuilder, if explain predicatebuilder.false() means opposed predicatebuilder.true()
it creates expression resolves false
if invoked instead of true
...
note false || variable
equivalent variable
. reason want initialize false avoid special-casing first actual check want initializing predicatebuilder
there. allows each of variables consistent , or
whatever predicatebuilder resolves to.
what compile method do?
it compiles expression delegate can invoked.
Comments
Post a Comment