c# - Replace part of a LINQ to SQL query with a reusable method -


we have introduced new feature in our application, affects hundreds of queries. have set bool field indicate if license valid, in complicated way.

i create method returning bool value, , i'd use in every query. problem is, if use in way shown below, executes separate query each result.

how use expression in way compiled sql , executed single query?

original query, in need of improvement

iqueryable<deviceminimal> devices =      device in db.devices     device.accountid = accountid      select new deviceminimal     {         id = device.id,         name = device.name,         licenseisvalid = !checkforlicense ||              device.license != null && (                 !device.license.trialstarted                 // && 12+ licensing rules             )     }; 

checkforlicense bool indicates license not need checked. used in cases , necessary considered.

code, solves problem, provokes separate query each device

iqueryable<deviceminimal> devices =      device in db.devices     device.accountid = accountid      select new deviceminimal     {         id = device.id,         name = device.name,         licenseisvalid =              licensehelper.islicensevalid(checkforlicense).invoke(device)     }; 

the method use in query above:

public static func<device, bool> islicenseenabledandvalid(bool checkforlicense) {     return result => !checkforlicense ||                          result.license != null && (             !result.license.trialstarted             // && 12+ licensing rules         ); } 

if set dataloadoptions of datacontext before query , setup them right, should avoid subselect. similar this:

db.loadoptions.loadwith<devices>(p => p.license); 

tha default behaviour (lazy loading of entities). have more info searching 'linq sql eagerloading'


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -