2017-02-12 55 views
0

我需要幫助來解決此問題。 我已經使用控制檯應用程序來解決這個問題,我卡住了。在C#列表中使用相同的組計算行,並將其設置爲其他屬性的int值

ORDER中的每一行都需要在一個相同的GROUP-value組中具有唯一值。組內ORDER的最大值必須爲行數-1,最小值必須爲0.排序並不重要。

它只需要是最小值和最大值之間的唯一值。

示例:或041532

ID GROUP VALUE ORDER 
1 1  10  0 
2 1  2  0 
3 2  1  0 
4 3  2  0 
5 3  6  0 
6 3  1  0 
7 3  9  0 

組1有2(-1)行,ORDER值必須0-1。
GROUP 2有1(-1)行,ORDER值必須爲0.
GROUP 3有4(-1)行,ORDER值必須爲0-3。

結束結果:

ID GROUP VALUE ORDER 
1 1  10  0 
2 1  2  1 
3 2  1  0 
4 3  2  0 
5 3  6  1 
6 3  1  3 
7 3  9  2 

這裏是我已經使用的屬性。

public class OrderRow 
{ 
    public int ID { get; set; } 
    public int GROUP { get; set; } 
    public int VALUE { get; set; } 
    public int ORDER { get; set; } 
} 

      new OrderRow {ID = 1, GROUP = 1, VALUE = 10, ORDER = 0}, 
      new OrderRow {ID = 2, GROUP = 1, VALUE = 2, ORDER = 0}, 
      new OrderRow {ID = 3, GROUP = 2, VALUE = 1, ORDER = 0}, 
      new OrderRow {ID = 4, GROUP = 3, VALUE = 2, ORDER = 0}, 
      new OrderRow {ID = 5, GROUP = 3, VALUE = 6, ORDER = 0}, 
      new OrderRow {ID = 6, GROUP = 3, VALUE = 1, ORDER = 0}, 
      new OrderRow {ID = 7, GROUP = 3, VALUE = 9, ORDER = 0}, 

THANKS

回答

0

我會去像如下:

 List<OrderRow> orders = new List<OrderRow>() { 
      new OrderRow { ID = 1, GROUP = 1, VALUE = 10, ORDER = 0 }, 
      new OrderRow { ID = 2, GROUP = 1, VALUE = 2, ORDER = 0 }, 
      new OrderRow { ID = 3, GROUP = 2, VALUE = 1, ORDER = 0 }, 
      new OrderRow { ID = 4, GROUP = 3, VALUE = 2, ORDER = 0 }, 
      new OrderRow { ID = 5, GROUP = 3, VALUE = 6, ORDER = 0 }, 
      new OrderRow { ID = 6, GROUP = 3, VALUE = 1, ORDER = 0 }, 
      new OrderRow { ID = 7, GROUP = 3, VALUE = 9, ORDER = 0 }, 
     }; 

     foreach (var item in orders.GroupBy(order => order.GROUP)) 
     { 
      int order = 0; 
      foreach (var item2 in item) 
      { 
       item2.ORDER = order++; 
      } 
     } 
+0

謝謝!! :) @ user3598756 –

+0

不客氣。如果我的答案解決了您的問題,您可能想要接受它!謝謝 – user3598756

相關問題