在C#中,你能在一个范围选择内执行另一个范围选择吗?

4
我最终想要在一个列表中获得当前年份的所有日期。
我有以下代码可以给出特定月份的所有日期...
    using System;
    using System.Collections.Generic;
    using System.Linq;
    internal class Program
    {
        public static void Main(String[] args)
        {
            List<DateTime> ldtDates = new List<DateTime>();
            Random rnd = new Random();
            int intCurrentYear = DateTime.Now.Year;
            int intCurrentMonth = DateTime.Now.Month;

            List<DateTime> DatesThisMonth =
                Enumerable.Range(1, DateTime.DaysInMonth(intCurrentYear, intCurrentMonth))
                          .Select(i => new DateTime(intCurrentYear, intCurrentMonth, i))
                          .ToList();

            foreach (var q in DatesThisMonth)
            {
                Console.WriteLine(q);
            }
            Console.WriteLine("Press <enter> to continue");
            Console.ReadLine();
        }
    }

这适用于一个月,但我想将Range(1,12)包装在这段代码周围,像这样:
using System;
using System.Collections.Generic;
using System.Linq;
internal class Program
{
    public static void Main(String[] args)
    {
        List<DateTime> ldtDates = new List<DateTime>();
        Random rnd = new Random();
        int intCurrentYear = DateTime.Now.Year;
        int intCurrentMonth = DateTime.Now.Month;

        var DatesThisYesr =
            Enumerable.Range(1, 12).Select(y=>
            Enumerable.Range(1, DateTime.DaysInMonth(intCurrentYear, y))
                      .Select(i => new DateTime(intCurrentYear, intCurrentMonth, i))
                      ).ToList();

        foreach (var q in DatesThisYesr)
        {
            Console.WriteLine(q);
        }
        Console.WriteLine("Press <enter> to continue");
        Console.ReadLine();
    }
}

这是我的输出结果:
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Int32,System.DateT
ime]
Press <enter> to continue

我可以使用 For 循环,但是也可以通过 Linq 实现这个功能。
谢谢。

1
你展示了你尝试的内容,很好!但是你没有说明为什么它失败了(显然)。 - Gert Arnold
1
谢谢,我刚刚添加了我失败尝试的输出。 - codingguy3000
2个回答

5

像这样使用 SelectMany:

var DatesThisYesr =
    Enumerable.Range(1, 12)
    .SelectMany(month =>
        Enumerable.Range(1, DateTime.DaysInMonth(intCurrentYear, month))
        .Select(day => new DateTime(intCurrentYear, month, day)))
    .ToList();

3
您目前选择的是一个序列的序列 - 这就是为什么当您打印每个元素时,它显示为WhereSelectEnumeratableIterator的原因。我怀疑您实际上想要展开它,所以您应该使用SelectMany而不是Select。请注意,您不需要使用intCurrentMonth - 您需要使用y(这是一个奇怪的变量名称,实际上代表一个月份编号)。

不过,将其编写为查询表达式可能会更简单:

var dates = from month in Enumerable.Range(1, 12)
            from day in Enumerable.Range(1, DateTime.DaysInMonth(year, month))
            select new DateTime(year, month, day);

我不认为在这里调用ToList()有任何理由,因为你只是在遍历结果。注意使用有意义的范围变量名称使整个过程更加易读。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接