Enumerable.Range动态拉回几个月

时间:2015-11-10 19:27:00

标签: c# model-view-controller

我正在制作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");

这些查询的新功能,但感觉必须有一种方法可以在查询本身中执行此操作。

提前致谢!

3 个答案:

答案 0 :(得分:2)

不是一个非常紧凑的代码,但可能会帮助你

unname(sum(unlist(z)*coefficients(x)[-1])+coefficients(x)[1])
[1] 2.036906 # same estimated value for z

修改

如果您需要月份名称列表

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

答案 1 :(得分:1)

在查询中执行此操作的另一种方法:

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

答案 2 :(得分:0)

使用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月