简体   繁体   中英

In C# can you perform a Range Select within a Range Select?

I ultimately want to get every date for the current year in a List.

I have the following which gives me all dates for a given month..

    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();
        }
    }

This works for the month, however I want to wrap a Range(1,12) around this code like this:

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();
    }
}

Here is what my output looks like:

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

I could use a For loop, however it should be possible to do this via Linq.

Thanks,

Use SelectMany like this:

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

You're currently selecting a sequence of sequences - which is why when you print each element out, it's showing that it's a WhereSelectEnumeratableIterator . I suspect you actually want to just flatten that, so you should use SelectMany instead of Select . Note that you don't want to use intCurrentMonth though - you want to use y (which is an odd name for a variable which effectively represents a month number).

It would probably be simpler to write this as a query expression though:

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);

I don't see any reason to call ToList() here, given that you're just iterating over the result. Note how the use of meaningful range variable names makes the whole thing much more readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM