动态更改月份列表

时间:2018-09-25 10:39:16

标签: c# linq foreach

我陷入了要求。 我从chr9 21970950 . GCCCCCCGCAG GCCCCCGCAG,GCCCCCCCGCAG 5.75583e-14 . AB=0,0;ABP=0,0;AC=0,0;AF=0,0;AN=2;AO=6,4;CIGAR=1M1D9M,1M1I10M;DP=95;DPB=94.7273;DPRA=0,0;EPP=4.45795,11.6962;EPPR=4.35716;GTI=0;LEN=1,1;MEANALT=8,8;MQM=60,60;MQMR=60;NS=1;NUMALT=2;ODDS=44.2048;PAIRED=0,0;PAIREDR=0;PAO=0,0;PQA=0,0;PQR=0;PRO=0;QA=122,77;QR=1739;RO=79;RPL=0,0;RPP=16.0391,11.6962;RPPR=174.557;RPR=6,4;RUN=1,1;SAF=2,4;SAP=4.45795,11.6962;SAR=4,0;SRF=43;SRP=4.35716;SRR=36;TYPE=del,ins;technology.non_illumina=1,1 **GT**:GQ:DP:AD:RO:QR:AO:QA:GL **0/0**:138.777:95:79,6,4:79:1739:6,4:122,77:0,-14.6086,-145.539,-18.0596,-141.62,-149.589 得到两个标志,分别是databaselastMonthNumber,它们的范围可以从lastMonthName1 to 12。 现在我有一个要求,如果January to DecemberlastMonthName="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 12的范围也可以是lastMonthName。父Jan to Dec必须是动态的。

如果listlastMonthNumber=6,则lastMonthName="April"需要包含6个元素,其中April为12,回溯以包含总共6个元素。

父级list可以是list,例如:

dictionary

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

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

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

5 个答案:

答案 0 :(得分:3)

具有一个带有月份名称的常量

private readonly string[] monthNames = { "Januar" ..., "December" };

和能够循环索引的访问器方法:

private string GetMonthName(int index)
{
  if (index < 0) return monthNames[monthNames.Length - 1 - (index % monthNames.Length)];
  return monthNames[index % monthNames.Length];
}

然后创建列表:

int indexOfLastMonthName = Array.IndexOf(monthNames, lastMonthName);

var parentData = Enumerable
  // Create as many items as last month number
  .Range(0, lastMonthNumber)

  // Create the list in reverse order, start on LastMonthName and 12,
  // then go back with every item
  .Select(x => new 
  { 
    Number = 12 - x, 
    Name = GetMonthName(indexOfLastMonthName - x)
  }

  // Turn it back
  .Reverse();

然后将其填充到适当的数据结构中。例如像这样:

List<Tuple<int, string>> parentList = parentData
  .Select(x => Tuple.Create(x.Number, x.Name)))
  .ToList();

如果您喜欢经典的解决方案(使用上面的GetMonthName方法):

int indexOfLastMonthName = Array.IndexOf(monthNames, lastMonthName);
List<Tuple<int, string>> parentList = new List<Tuple<int, string>>();

for(int i = 0; i < lastMonthNumber; i++)
{
  parentList.Add(
    Tuple.Create(
      12 - i, 
      GetMonthName(indexOfLastMonthName - i));
}

parentList.Reverse();

答案 1 :(得分:1)

尝试此linq查询

var months = new List<string> {"January","February","March","April","May","June","July","August","September","October","November","December" };

var lastMonthNumber = ...;
var lastMonthName = ....;

var rows = months.Concat(months)
    .Reverse()
    .SkipWhile(m => m != lastMonthName)
    .Take(lastMonthNumber)
    .Reverse()
    .Select((m, i) => new { id = i + 12 - lastMonthNumber + 1, m})
    .ToArray();

它重复了一个月列表,将其反转并跳过项目直到找到lastMonthName。此后,此代码限制的结果项数为lastMonthNumber,并反向列出

答案 2 :(得分:0)

    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" };
int flag=0;
int items=12;
var numbersList = new List<int>();
var monthsList = new List<string>(); 
     foreach (var month in months)
         {
            if(flag==0){
             monthsList.Insert(items--,month);
             if (month == lastMonthName)
                {
                    flag=1;
                }
}
         else if(flag==1)
         {
           monthsList.add(month);
          }
          }
flag=0;

答案 3 :(得分:0)

尝试下面的小提琴。 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); 
}

答案 4 :(得分:0)

尝试以下操作:

        var months = Enumerable.Range(1, 12).Select(i => new
        {
            I = i,
            M = System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i)
        });

        //string lastMonthName = "March"; int lastMonthNumber = 12;
        //string lastMonthName = "April"; int lastMonthNumber = 6;
        var selMonthInt = months.Where(x => x.M == lastMonthName).Select(y => y.I).FirstOrDefault();
        int endCount = lastMonthNumber + selMonthInt;
        if (endCount >= 12) { endCount = selMonthInt; }

        var lst1 = months.Where(x => x.I > endCount).Select(z => z.M);
        var lst2 = months.Where(x => x.I <= selMonthInt).Select(z => z.M);
        var lst = lst1.Union(lst2).ToArray();

        var selMonths = Enumerable.Range(0, lastMonthNumber).Select(i => new { I = (13 - lastMonthNumber + i), M = lst[i] });