我最终希望获得列表中当年的每个日期。
我有以下内容可提供给定月份的所有日期。
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做到这一点。
谢谢,
SelectMany
像这样使用:
var DatesThisYesr =
Enumerable.Range(1, 12)
.SelectMany(month =>
Enumerable.Range(1, DateTime.DaysInMonth(intCurrentYear, month))
.Select(day => new DateTime(intCurrentYear, month, day)))
.ToList();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句