LINQ - Week of a month in LINQ - Asked By Shan on 23-May-11 03:02 AM

Hi,

  How to fetch the week of the month using LINQ.

 In sql I am using this

  select datediff(week, convert(varchar(6), getDate(), 112) + '01', getDate()) + 1

Ravi S replied to Shan on 23-May-11 03:08 AM
HI

try this

public static List<DateTime> GetDates(int year, int month)
{
   return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
                    .Select(day => new DateTime(year, month, day))
                    .Where(dt => dt.DayOfWeek != DayOfWeek.Sunday &&
                                 dt.DayOfWeek != DayOfWeek.Saturday)
                    .ToList();
}
Ravi S replied to Shan on 23-May-11 03:09 AM
HI

use this example

public static int GetWeekInMonth(DateTime date)
      {
        DateTime tempdate = date.AddDays(-date.Day + 1);

        CultureInfo ciCurr = CultureInfo.CurrentCulture;
        int weekNumStart = ciCurr.Calendar.GetWeekOfYear(tempdate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
        int weekNum = ciCurr.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

        return weekNum - weekNumStart + 1;

      }