c# - Find Closest Week Day in DayOfWeek List -
this newbie question, here goes.
i have method type dayofweek list gets appended various days of week (could wednesday & saturday, sunday, monday, & friday, etc).
given list, need compare datetime parameter, find week day datetime parameter closest in dayofweek list, , add days datetime parameter based on week day in list.
for example, if datetime parameter being passed in sunday, , dayofweek list contains wednesday , saturday, parameter needs moved saturday since closest in list.
similarly, if list contains sunday, monday, , saturday, , parameter passed in thursday, parameter have moved saturday.
finally, if parameter equidistant 2 week days in list (wednesday passed in , monday , friday in list... or sunday passed in , tuesday , friday in list), parameter needs moved forward next closest week day (which, in first case, friday, , tuesday in second case).
it ideal (at least me), convert distance of next closest week day passed in date int, way can like:
passedindate = passedindate.adddays(dayofweekdistance); return passedindate; but open suggestions.
i have tried linq statements such as:
int dayofweekdistance = targetdayofweeklist.min(x => (x - passedindate)); but no avail. there has fancy linq statements i'm missing.
just heads up, main item can't work date backtrack sunday saturday if passed in date sunday , closest week day in list saturday (similarly, if passed in date monday , closest week day friday, date need traverse way friday).
please let me know if missed or i'm plain not making sense.
all welcome! thanks.
with helper function, linq can used.
the helper function computes closest day of week using utility function compute number of forward days between 2 dows:
public int mindowdistance(dayofweek dow1, dayofweek dow2) { int fwddaysdiff(int idow1, int idow2) => idow2 - idow1 + ((idow1 > idow2) ? 7 : 0); int fwd12 = fwddaysdiff((int)dow1, (int)dow2); int fwd21 = fwddaysdiff((int)dow2, (int)dow1); return fwd12 < fwd21 ? fwd12 : -fwd21; } then can find nearest dow in list , return right number of days move (and direction) using aggregate linq:
public int daystoclosestdow(dayofweek dow1, list<dayofweek> dowlist) { return dowlist.select(dow => { var cdow = mindowdistance(dow1, dow); return new { dow, dist = cdow, absdist = math.abs(cdow) }; }) .aggregate((g1, g2) => (g1.absdist < g2.absdist) ? g1 : ((g1.absdist == g2.absdist) ? ((g1.dist > 0) ? g1 : g2) : g2)).dist; } it occurred me use tuple return absdist helper function since knows it. can use tuple in linq:
public (int dist, int absdist) mindowdistance(dayofweek dow1, dayofweek dow2) { int fwddaysdiff(int idow1, int idow2) => idow2 - idow1 + ((idow1 > idow2) ? 7 : 0); int fwd12 = fwddaysdiff((int)dow1, (int)dow2); int fwd21 = fwddaysdiff((int)dow2, (int)dow1); if (fwd12 < fwd21) return (fwd12, fwd12); else return (-fwd21, fwd21); } public int daystoclosestdow(dayofweek dow1, list<dayofweek> dowlist) { return dowlist.select(dow => mindowdistance(dow1, dow)) .aggregate((g1, g2) => (g1.absdist < g2.absdist) ? g1 : ((g1.absdist == g2.absdist) ? ((g1.dist > 0) ? g1 : g2) : g2)).dist; }
Comments
Post a Comment