1

我有一个包含复杂数据的列表

public class CAR
{    
public int ID {get ; set ; }
public string Name { get ; set ; }
public string EngineType { get ; set ; }
public List<string> Months { get; set; }

}

请注意,Months数据是List<string>它的最大计数是150

List<CAR> A = new List<CAR>();
List<CAR> B = new List<CAR>();

A有以下数据

 ID  |  Name  | EngineType |  Months[0] | Months[1] |  Months[2] |  Months[3]  .. | Months[149] | 
   1   |  Zen   |  1001      |  1         | 1         |  4         |  5        ..   | 6           |
   2   |  Benz  |  2002      |  6         | 4         |  5         |  6        ..   | 2           |
   3   |  Zen   |  1001      |  3         | 1         |  7         |  5        ..   | 0           |
   4   |  Zen   |  1001      |  2         | 2         |  4         |  5        ..   | 6           |
   5   |  Zen   |  2002      |  2         | 2         |  4         |  5        ..   | 6           |
   6   |  Benz  |  2002      |  1         | 1         |  1         |  1        ..   | 1           |

如果EngineTypeName相同,我们添加这些行并将结果存储在单行中例如:添加行

row 1 in B =     1 + 3 + 4
row 2 in B =     2 + 6
row 3 in B =     5

B应包含以下操作

   ID  |  Name  | EngineType |  Months[0] | Months[1] |  Months[2] |  Months[3] ... | Months[149] | 
   -   |  Zen   |  Petrol    |  6         | 4         |  15        |  15       ..   | 12          |
   -   |  Benz  |  Diesel    |  7         | 5         |  6         |  7             | 3           |        
   -   |  Zen   |  Diesel    |  2         | 2         |  4         |  5        ..   | 6           |

如果月份数据是整数类型的单独实体,我本可以这样做

B = from val in A 
      group val by  new val.EngineType into g 
          select new CAR{
        EngineType = g.Key, 
        Name = g.Name,
                Month0 = g.Sum(p => p.Month0),
                Month1 = g.Sum(p => p.Month1),
                Month2 = g.Sum(p => p.Month2),
            .
        .
        .
        .
        .
        .
                Month148 = g.Sum(p => p.Month148),
                Month149 = g.Sum(p => p.Month149)
}.ToList<CAR>();

但是由于它的类型List<string>有没有办法做到这一点?非常感谢!

4

2 回答 2

3

使用 LINQ 的强大功能:

var B = A.GroupBy(x => new { x.Name, x.EngineType })
         .Select(g => new Car
         {
             Name = g.Key.Name,
             EngineType = g.Key.EngineType,
             Months = g.SelectMany(x => x.Months.Select((y,i) => new { i, y = int.Parse(y) }))
                       .GroupBy(x => x.i)
                       .OrderBy(g2 => g2.Key)
                       .Select(g2 => g2.Sum(x => x.y).ToString()).ToList()
         }).ToList();
于 2013-07-23T17:26:32.743 回答
0
        foreach (CAR c in A)
        {
            bool blnadded = false;
            if (B.Count == 0)
            {
                B.Add(c);
                blnadded = true;
            }
            else
                foreach (CAR d in B)
                {
                    if (d.Name == c.Name && d.EngineType == c.EngineType)
                    {
                        for (int i = 0; i < d.Months.Count; i++)
                            d.Months[i] = (Convert.ToInt32(d.Months[i]) + Convert.ToInt32(c.Months[i])).ToString();
                        blnadded = true;
                    }
                }
            if (blnadded==false)
                B.Add(c);
        }
于 2013-07-23T17:55:24.277 回答