2017-05-19 49 views
-1

我有一個數組列表。其中包含以逗號分隔的月份編號和數據。 現在我想遍歷這個列表並檢查是否有任何月份號碼丟失。如果之後需要按照數據部分爲零的順序添加該月的數字。如何將缺失的數字添加到列表中

List<string> MyData = new List<string>(); 
    MyData.Add("4,500"); //dynamically adding. 4(april) and 6(june) are month numbers. 
    MyData.Add("6,400"); 
    foreach (string str in MyData) 
     { 
    int GetmonthNum = Convert.ToInt32(str.Substring(0, str.IndexOf(','))); 
     } 

現在需要添加所有其他缺少月份數值爲零。將得到的名單應該是

"4,500","5,0","6,400","7,0","8,0","9,0","10,0","11,0","12,0","1,0","2,0","3,0" 

回答

1

您可以使用contains這樣

 var result = new List<string>(); 
     for (int i = 1; i <= 12 ; i++) 
     { 
      var firstMatch = myData.FirstOrDefault(x => x.Contains(i + ",")); 

      if (firstMatch == null) 
      { 
       result.Add(i + ",0"); 
      } 
      else 
      { 
       result.Add(firstMatch); 
      } 
      // or short code: result.Add(firstMatch ?? i + ",0"); 
     } 

如果你想「4500」是那麼的第一項嘗試

 var minMonth = myData.Min(x => Convert.ToInt32(x.Substring(0, x.IndexOf(",", StringComparison.CurrentCulture)))); 

     var result = new List<string>(); 
     for (int i = minMonth - 1; i < minMonth + 11; i++) 
     { 
      var firstMatch = myData.FirstOrDefault(x => x.StartsWith((i % 12) + 1 + ",")); 
      result.Add(firstMatch ?? (i % 12) + 1 + ",0"); 
     } 
+0

有沒有什麼辦法讓 「4500」, 「5,0」 所得到的輸出, 「6400」, 「7,0」, 「8,0」, 「9,0」, 「10.0」, 「11.0」, 「12.0」, 「1,0」,「2,0 「,」3,0「格式? – user2431727

+0

是否要將List轉換爲字符串或將「4,500」設置爲第一項? – TriV

+0

想要設置第一項作爲自己。然後按順序重複其他月份。 EX:如果第一個項目是四月。那麼新的數組列表應該是從四月,五月,六月,...行軍 – user2431727

0

在情況下,如果有您的名單中有12件[分別爲12個月]請嘗試以下代碼

List<string> MyData = new List<string>(); 
      MyData.Add("4,500"); //dynamically adding. 4(april) and 6(june) are month numbers. 
      MyData.Add("6,400"); 
      int i = 1; 
      // use variable i from 1 to 12 as month indicator 
      foreach (string str in MyData) 
      { 
       string month = str.Substring(0, str.IndexOf(',')); 
       // get the month from your list item here 
       int GetmonthNum = Convert.ToInt32(month==string.Empty || month==null ? i.ToString() : month ); 
       // here use conditional operator to check if month is not there in list item , if it is not present than return i as misisng month 
       i++; 
      } 


讓我知道如果你把代碼放在一起

0

我是能夠實現與下面的循環你想要的結果感到任何問題。雖然有很多方法可以做到。

 int arrayIndex = 0; 
     int month = 1; 

     for (int i = 0; i < 12; i++) 
     { 
      if (myArray[arrayIndex].Split(',')[0] == Convert.ToString(month)) 
      { 
       MyData.Add(myArray[arrayIndex]); 
       month++; 
       arrayIndex++; 
      } 
      else 
      { 
       MyData.Add(Convert.ToString(month) + ",0"); 
       month++; 
      } 

     } 
0
 List<string> MyData = new List<string>(); 
     MyData.Add("4,500"); 
     MyData.Add("6,400"); 

     var months = Enumerable.Range(1, 12); 
     foreach (int month in months) 
     { 
      if (MyData.Any(a => a.Split(',')[0] == month.ToString())) 
      continue; 
      MyData.Add(string.Format("{0},{1}", month.ToString(), "0")); 
     } 
0

這工作:

MyData = 
    MyData 
     .Select(x => x.Split(',').Select(y => int.Parse(y)).ToArray()) 
     .Concat(Enumerable.Range(1, 12).Select(x => new [] { x, 0 }).ToArray()) 
     .OrderBy(x => x[0]) 
     .GroupBy(x => x[0], x => x[1]) 
     .SelectMany(x => x.Take(1), (y, z) => $"{y.Key},{z}") 
     .ToList(); 

這一點讓:

 
1,0 
2,0 
3,0 
4,500 
5,0 
6,400 
7,0 
8,0 
9,0 
10,0 
11,0 
12,0 
相關問題