2016-10-01 61 views
1

記錄我加載CSV文件集團通過從csv文件

enter image description here

分爲:

public class ReleaseVersion 
{ 
    public string Version { get; set; } 
    public DateTime? ReleaseDate { get; set; } 
    public IEnumerable<ReleaseNote> ReleaseNotes { get; set; } 

} 

public class ReleaseNote 
{ 
    public string Description { get; set; } 
    public string Progress { get; set; } 
    public string Status { get; set; } 
} 

哪能組通過的版本中的所有記錄,並返回ReleaseVersion的名單?

這是我所做的,但是我不知道如何通過第一列進行分組,即文件中的版本列。

var records = csvLinesData.Select(data => new ReleaseVersion 
     { 

      Version = data[0], 
      ReleaseDate = TryParse(data[1]), 
      // add list of ReleaseNote ?? 
     }).ToList(); 

回答

4

剛上第一項GroupBy

DateTime date; 

    var records = csvLinesData 
    .GroupBy(line => line[0]) // line[0] is version we want to group by on 
    .Select(chunk => new ReleaseVersion() { 
     Version = chunk.Key, 
     ReleaseDate = DateTime.TryParse(chunk.First()[1], out date) 
      ? (DateTime?) date 
      : null, 
     ReleaseNotes = chunk 
     .Select(item => new ReleaseNote() { 
      Description = item[2], 
      Progress = item[3], 
      Status = item[4], }) 
     .ToArray(), }) 
    .ToList();