簡體   English   中英

Enumerable.Range動態地拉回幾個月

[英]Enumerable.Range to pull months back dynamically

我正在研究一個MVC框架報告,我們試圖將用戶限制為兩年的數據(從今天開始)。 根據他們的要求,我們有一個月份和年份dropDownList,用戶只能看到兩年的數據。 因此,目前,如果他們選擇2013年,他們只會看到11月和12月。

得到了DDL填充年份,現在我正在嘗試根據選擇的年份填充月份列表。

以下是我所有月份的原因:

var months = Enumerable.Range(1, DateTime.Today.Month)
    .Select(x => new SelectListItem { Text = x.ToString(), Value = x.ToString() });

return new SelectList(months.ToList(), "Value", "Text");

這些查詢的新功能,但感覺必須有一種方法在查詢本身內執行此操作。

提前致謝!

不是一個非常緊湊的代碼,但可能會幫助你

List<int> months = new List<int>();
int year = 2013; //selected year
var today = DateTime.Now;

if (year == today.Year)
    months = Enumerable.Range(1, today.Month).ToList();
else if (year == today.Year - 1)
    months = Enumerable.Range(1, 12).ToList();
else if (year == today.Year - 2)
    months = Enumerable.Range(today.Month, 12 - (today.Month - 1)).ToList();

編輯

如果您需要月份名稱列表

//using System.Globalization;
string[] monthNames = DateTimeFormatInfo.CurrentInfo.MonthNames; //all month names
List<string> ddMonths = monthNames.Where((m, idx) => months.Contains(idx + 1)).ToList();

在查詢中執行此操作的另一種方法:

Console.Write("Enter a year: ");
int year = int.Parse(Console.ReadLine());
int maxYearsBack = 2;

var months = Enumerable.Range(1, 12).Where(m =>
    (DateTime.Today.Year == year && m <= DateTime.Today.Month) || // This year
    (DateTime.Today.Year != year && (DateTime.Today.Year - year < maxYearsBack || m - DateTime.Today.Month >= 0)) // Middle and max years back
).Select(x => x.ToString());

Console.WriteLine("Months for year {0}: {1}", year, String.Join(", ", months));

隨着輸出:

Enter a year: 2015
Months for year 2015: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
Enter a year: 2014
Months for year 2014: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
Enter a year: 2013
Months for year 2013: 11, 12

如果你想要名字,只需將最后一個select更改為:

...).Select(x => new SelectListItem { Text = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x), Value = x.ToString() });

使用Linq,您可以在一行代碼中連接三個不同的月份列表:

1 - 今年剩余的月份

2 - 整個明年

3 - 次年的月份直到起始月份

試一試:

static void Main(string[] args)
    {
        var list =
            System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.Select(s => s + " " + DateTime.Today.Year)    // Get now through the end of the year
                    .Skip(DateTime.Today.Month - 1)
                    .Take(12 - (DateTime.Today.Month - 1))
                    .ToList()
                .Concat(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.Select(s => s + " " + DateTime.Today.AddYears(1).Year).Take(12))  // Tack on next year
                .Concat(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.Select(s => s + " " + DateTime.Today.AddYears(2).Year)    // Tack on the last months
                    .Take(DateTime.Today.Month - 1))
                    .ToList();

        foreach (var item in list)
        {
            Console.WriteLine(item);
        }
        Console.ReadLine();
    }

這是控制台輸出:

2015年11月

2015年12月

2016年1月

2016年2月

2016年3月

2016年4月

2016年5月

2016年6月

2016年7月

2016年8月

2016年9月

2016年10月

2016年11月

2016年12月

2017年1月

2017年2月

2017年3月

2017年4月

2017年5月

2017年6月

2017年7月

2017年8月

2017年9月

2017年10月

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM