c# - How to loop or get a last week day from current date -
c# - How to loop or get a last week day from current date -
i need day of lastly week current day (current day = day of current week) linq selection based on lastly week. below code:
public class { dayofweek fri = dayofweek.friday; datetime today = datetime.today; public ienumerable<entity> dosomething() { var context = new entity(); var results = new list<entity>(); if (friday.tostring() == today.dayofweek.tostring()) { string tuesdaydate = today.adddays(-3).toshortdatestring(); datetime _tuesdaydate = convert.todatetime(tuesdaydate); result = (from in context.entity a.rdate == _tuesdaydate //this tuesday of current week select a).tolist(); homecoming result.tolist(); } else { //---here problem //do linq selection //where rdate == tuesday of lastly week //--what logic write here? } homecoming result; } }
i wrote blog post on issue long time ago, situation find next day of week given start date. here's code:
c#
public static datetime getnextdateforday( datetime startdate, dayofweek desiredday ) { // given date , day of week, // find next date day of week equals specified day of week. homecoming startdate.adddays( daystoadd( startdate.dayofweek, desiredday ) ); } public static int daystoadd( dayofweek current, dayofweek desired ) { // f( c, d ) = g( c, d ) mod 7, g( c, d ) > 7 // = g( c, d ), g( c, d ) < = 7 // 0 <= c < 7 , 0 <= d < 7 int c = (int)current; int d = (int)desired; int n = (7 - c + d); homecoming (n > 7) ? n % 7 : n; }
for problem, this:
datetime lasttuesday = getnextdateforday(datetime.today, dayofweek.tuesday).adddays(-7);
c# linq
Comments
Post a Comment