如果第2个星期六出现在前9天,则获取月份的第1个和第3个星期六的清单

时间:2016-10-12 09:38:14

标签: c# linq c#-4.0 linq-to-sql linq-to-entities

我希望获得所有第二名和第二名的名单。一年的第四个星期六。但是如果第二个星期六出现在一个月的前9天,那么返回那个月的第3个和第5个星期六的清单。到目前为止,我得到了所有第二和第二的列表全年第4个星期六。但是如果第二个星期六出现在一个月的前9天,我就无法获得第3和第5个星期六。

class Program
{
    static void Main(string[] args)
    {
        List<DateTime> MyCalendar = new List<DateTime>(); //create list
        DateTime currDate = new DateTime(2017, 1, 1); //initial value
        //add days to MyCalendar
        while (currDate <= new DateTime(2017, 12, 31))
        {
            MyCalendar.Add(currDate);
            currDate = currDate.AddDays(1);
        }
        //method to get 2. and 4. saturday in month
        var result = MyCalendar.Where(x => x.DayOfWeek == DayOfWeek.Saturday)
                        .GroupBy(x => x.Month)
                        .SelectMany(grp =>
                            grp.Select((d, counter) => new
                            {
                                Month = grp.Key,
                                PosInMonth = counter + 1,
                                Day = d
                            }))

                        .Where(x => x.PosInMonth == 2 || x.PosInMonth == 4)
                        .ToList();

        foreach (var d in result)
        {
            Console.WriteLine("{0} {1} {2}", d.Month, d.PosInMonth, d.Day);
        }

        Console.Read();

    }

}

推出该计划

Out of Program

3 个答案:

答案 0 :(得分:1)

另一种方法

            DateTime currDate = new DateTime(2017, 1, 1); //initial value
            int dayOfWeek = (int)currDate.DayOfWeek;
            currDate = currDate.AddDays(6 - dayOfWeek);
            //add days to MyCalendar
            while (currDate <= new DateTime(2017, 12, 31))
            {
                MyCalendar.Add(currDate);
                currDate = currDate.AddDays(7);
            }
            var result = MyCalendar.GroupBy(x => x.Month).Select(x => x.Skip(1).First().Day <= 9 ? new DateTime[] { x.Skip(2).First(), x.Skip(4).First() } : new DateTime[] { x.Skip(1).First(), x.Skip(3).First() }).SelectMany(x => x).ToList();

            foreach (var d in result)
            {
                Console.WriteLine("{0} {1}", d.Month, d.Day);
            }

            Console.Read();

答案 1 :(得分:0)

这个怎么样?

using System;
using System.Collections.Generic;

namespace Demo
{
    class Program
    {
        static void Main()
        {
            foreach (var saturday in FilteredSaturdays(DateTime.Now, new DateTime(2017, 12, 31)))
            {
                Console.WriteLine(saturday);
            }
        }

        public static IEnumerable<DateTime> FilteredSaturdays(DateTime start, DateTime end)
        {
            DateTime startMonth = new DateTime(start.Year, start.Month, 1);
            DateTime endMonth = new DateTime(end.Year, end.Month, 1).AddMonths(1);

            for (DateTime month = startMonth; month < endMonth; month = month.AddMonths(1))
            {
                // Work out date of last saturday in the month.

                DateTime lastDayOfMonth = month.AddMonths(1).AddDays(-1);
                DateTime lastSaturdayOfMonth = lastDayOfMonth.AddDays(-(((int)lastDayOfMonth.DayOfWeek+1)%7));

                // Return saturday 2 weeks before last saturday of month, and last saturday of month.

                yield return lastSaturdayOfMonth.AddDays(-14);
                yield return lastSaturdayOfMonth;
            }
        }
    }
}

这比看一年中的每一天看看是否是星期六更有效率。

[编辑]似乎实际要求是你想要每个月的最后一个星期六以及星期六前两个星期的星期六,所以我已经相应地更新了我的解决方案。

答案 2 :(得分:0)

这是一个快速而肮脏的解决方案;我承认它可以更加优化。

将执行查找的方法:

private static IEnumerable<DateTime> GetWeekDayOfMonth(DateTime monthToCheck, DayOfWeek weekDayToFind)
{
    var year = monthToCheck.Year;
    var month = monthToCheck.Month;
    var dayCount = DateTime.DaysInMonth(year, month);
    var daysList = Enumerable.Range(1, dayCount)
                                    .Select(day => new DateTime(year, month, day))
                                    .Where(date => date.DayOfWeek == weekDayToFind)
                                    .ToList<DateTime>();
    // Loop with 2 increment
    int lookupStart = 1;
    int loopCount = 0;
    if (daysList[1].Day <= 9)
    {
        lookupStart = 2;
    }

    for (var i = lookupStart; i < daysList.Count(); i = i + 2) 
    {
        if (loopCount < 2)
        {
            yield return daysList[i];
            loopCount++;
        }
    }
}

以下是调用它的代码:

private static void FindWeekDays()
{
    DateTime dateToCheck = new DateTime(2017, 1, 1);
    List<DateTime> dayList = new List<DateTime>();
    while (dateToCheck.Year <= 2017)
    {
        dayList.AddRange(GetWeekDayOfMonth(dateToCheck, DayOfWeek.Saturday));
        dateToCheck = dateToCheck.AddMonths(1);
    }

    dayList.ForEach(date => Console.WriteLine(date.ToString()));
}

主要:

static void Main(string[] args)
{
    FindWeekDays();
}

将为您提供年度2017的以下结果(可以更改):

Dates