c# - Optimize LINQ query inside a loop -


i have piece of code ensures customer's addresses in sync modifications made in ui:

var customeraddresses = customer.customeraddresses.select(x => x.address);  // add address customer if not exist foreach (var addressmodel in model.addresses) {     // make sure address matches properties     func<address, bool> addressfilter = x => x.id == addressmodel.id &&              x.street.equals(addressmodel.street, stringcomparison.ordinalignorecase) &&              x.city.equals(addressmodel.city, stringcomparison.ordinalignorecase) &&              x.province.equals(addressmodel.province, stringcomparison.ordinalignorecase) &&              x.postalcode.equals(addressmodel.postalcode, stringcomparison.ordinalignorecase);      // check if customer has address     if (!customeraddresses.any(addressfilter))     {         // check if address exists in database         var address = this.dbcontext.addresses.singleordefault(addressfilter);          // add address if not exist         if (address == null)         {             address = this.dbcontext.addresses.add(new address             {                 street = addressmodel.street,                 city = addressmodel.city,                 province = addressmodel.province,                 postalcode = addressmodel.postalcode             });         }     }      this.dbcontext.customeraddresses.add(new insuredaddress     {         customer = customer,         address = address,         ispreferred = addressmodel.ispreferred     });              } 

i concerned func<address, bool> addressfilter created every time inside loop. there way create filter in such way accepts parameters , not need recreated each time?

it's easier see implications of once understand how anonymous methods dealt compiler.

the compiler going create new time, arbitrary name. give type instance method other arbitrary name. body of method body of anonymous method.

there instance field each closed on variable.

a new instance of type created within method, , closed on variables replaced access field of closure class. call anonymous method replaced call method in new class.

thus should able see, based on transformation, anonymous method ever compiled once, regardless of scope of definition in other method.


having said of that, really shouldn't structure program way entirely unrelated reasons. first off, you're defining func rather expression<func>, filter won't able translated query executed on database, rather you're pulling down the entire addresses table twice (once when call any, , once when call singleordefault, for every single address in model. that's really bad. mean @ very, least, should using expression define predicate filtering can done on database side of things, , omitting call any you're doing 1 query per loop, honestly, shouldn't performing multiple queries at all. should doing joining 2 tables of information whole thing in 1 big query, rather executing any queries in loop.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

android - Inheriting from Theme.AppCompat* -