entity framework core - Searching for a range of dates in LINQ -


i'm trying search through table via linq , entity framework core. i've got 2 text boxes startdate , enddate , radio button set of 3 options created, modified , both.

this code i've produced based on google searches , tutorials

       switch(radcreatedmodifiedboth) {             case "b":                 if (!string.isnullorempty(startdate)) {                     if (!string.isnullorempty(enddate)) {                         persons = persons.where(ps => (                                                         ps.createddate >= convert.todatetime(startdate + " 00:00:00") &&                                                         ps.createddate <= convert.todatetime(enddate + " 23:59:59")                                                         ) || (                                                         ps.modifieddate >= convert.todatetime(startdate + " 00:00:00") &&                                                         ps.modifieddate <= convert.todatetime(enddate + " 23:59:59")                                                         )                                                 );                     } else {                         persons = persons.where(ps => (                                                         ps.createddate >= convert.todatetime(startdate + " 00:00:00")                                                         ||                                                         ps.modifieddate >= convert.todatetime(startdate + " 00:00:00")                                                       )                                                 );                     }                 } else if (!string.isnullorempty(enddate)) {                     persons = persons.where(ps => (                                                     ps.createddate >= convert.todatetime(enddate + " 23:59:59")                                                     ||                                                     ps.modifieddate >= convert.todatetime(enddate + " 23:59:59")                                                   )                                             );                 }                 break;             case "c":                 if (!string.isnullorempty(startdate)) {                     if (!string.isnullorempty(enddate)) {                         persons = persons.where(ps => (                                                         ps.createddate >= convert.todatetime(startdate + " 00:00:00") &&                                                         ps.createddate <= convert.todatetime(enddate + " 23:59:59")                                                         )                                                 );                     } else {                         persons = persons.where(ps => (                                                         ps.createddate >= convert.todatetime(startdate + " 00:00:00")                                                       )                                                 );                     }                 } else if (!string.isnullorempty(enddate)) {                     persons = persons.where(ps <= (                                                     ps.createddate >= convert.todatetime(enddate + " 23:59:59")                                                   )                                             );                 }                 break;             case "m":                 if (!string.isnullorempty(startdate)) {                     if (!string.isnullorempty(enddate)) {                         persons = persons.where(ps => (                                                         ps.modifieddate >= convert.todatetime(startdate + " 00:00:00") &&                                                         ps.modifieddate <= convert.todatetime(enddate + " 23:59:59")                                                         )                                                 );                     } else {                         persons = persons.where(ps => (                                                         ps.modifieddate >= convert.todatetime(startdate + " 00:00:00")                                                       )                                                 );                     }                 } else if (!string.isnullorempty(enddate)) {                     persons = persons.where(ps <= (                                                     ps.modifieddate >= convert.todatetime(enddate + " 23:59:59")                                                   )                                             );                 }                 break;         } 

this code works seems massively inefficient, not mention adding start , end time date string

startdate + " 00:00:00" enddate + " 23:59:59" 

just seems wrong. prescribed method or can suggest more efficient method preferably getting rid of " 00:00:00"/" 23:59:59"

thanks

you can simplify pushing tests sql - conditional operator translated sql case when since aren't simple constants. note assumed have enddate tests backwards in code sample. have lot of repeated sub-expressions consolidated variables. since using ef core, there isn't better way handle date comparisons using. in ef can use dbfunction, still isn't converting dates appropriate date+time indices can used.

var hasstartdate = !string.isnullorempty(startdate); var dtstartdate = hasstartdate ? convert.todatetime(startdate + " 00:00:00") : datetime.minvalue;  var hasenddate = !string.isnullorempty(enddate); var dtenddate = hasenddate ? convert.todatetime(enddate + " 23:59:59") : datetime.minvalue;  var chkcreateddate = (radcreatedmodifiedboth == "b" || radcreatedmodifiedboth == "c"); var chkmodifieddate = (radcreatedmodifiedboth == "b" || radcreatedmodifiedboth == "m");  persons = persons.where(ps => (chkcreateddate ? (hasstartdate ? ps.createddate >= dtstartdate : true) && (hasenddate ? ps.createddate <= dtenddate : true) : true)                                ||                               (chkmodifieddate ? (hasenddate ? ps.modifieddate >= dtstartdate : true) && (hasenddate ? ps.modifieddate <= dtenddate : true) : true)                         ); 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -