this way i am looping in two days and generate file name and store in list. so i like to know is there any way by which i can avoid loop but generate file name like looping.
DateTime StartDate = DateTime.Now;
DateTime EndDate = DateTime.Now.AddDays(-7);
List<string> filename = new List<string>();
for (DateTime date = EndDate; date.Date <= StartDate.Date; date = date.AddDays(1))
{
filename.Add("Report_"+date.ToString("dd-MM-yyyy")+".csv");
}
thanks
答案 0 :(得分:4)
这算不算循环吗?
DateTime StartDate = DateTime.Now;
List<string> filename =
Enumerable
.Range(-7, 8)
.Select(n => "Report_" + StartDate.AddDays(n).ToString("dd-MM-yyyy") + ".csv")
.ToList();
答案 1 :(得分:1)
试试这个:
var filename = new int[8]
.Select((d, i) => string.Format("Report_{0:dd-MM-yyyy}.csv", DateTime.Now.AddDays(-i)))
.Reverse()
.ToList();