动态更改月份列表

DevAssasin

我陷入了要求。我从中获得两个标志database,分别是lastMonthNumberlastMonthName,这些标志的范围可以从1 to 12和开始January to December现在我有一个要求,如果iflastMonthName="March"lastMonthNumner=12,那么父级list应该如下:

1, April
2, May
3, June
4, July
5, August
6, September
7, October
8, November
9, December
10, January
11, February
12, March

如果lastMonthName="April"lastMonthNumber=6,则列表应为:

7, November
8, December
9, January
10, February
11, March
12, April

这个lastMonthNumber范围可以从1 to 12lastMonthName太可以从范围Jan to Dec父级list必须是动态的。

如果lastMonthNumber=6lastMonthName="April",则list需要有6个元素,April为12,回溯到总共6个元素。

父级list可以是dictionary,例如:

var monthsDictionary=new Dictionary<int, string>();

我正在尝试以下操作,但无法进一步可视化:

var monthsDictionary = new Dictionary<int, string>();
var numbers = new List<int> { 1,2,3,4,5,6,7,8,9,10,11,12};
var months = new List<string> {"January","February","March","April","May","June","July","August","September","October","November","December" };
 foreach (var month in months.Select())
     {
         if (month == lastMonthName)
            {

            }
      }

请帮忙。指针将非常有帮助。

迪帕克·沙玛(Deepak Sharma)

尝试下面的小提琴。https://dotnetfiddle.net/GYP2Go

private static Dictionary<int, string> GetRequiredResult(int lastMonthNumber, string lastMonthName)
{
    var indx =  months.IndexOf(lastMonthName);

    // here this list will have months in required order that ends with lastMonthName
    var revisedMonthList = new List<string>();
    revisedMonthList.AddRange(months.Skip(indx + 1).Take(12));
    revisedMonthList.AddRange(months.Take(indx + 1));

    // get count = lastMonthNumber element from last using index, and then convert them to dictionary.
    return revisedMonthList
        .Select((mn, index) => new {index, mn})
        .Where(c => c.index >= months.Count - lastMonthNumber)
        .ToDictionary(c=>c.index + 1, c=>c.mn); 
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章