尝试迭代csv并查找记录,如果当前共享相同RelatedOrder的记录列表中有记录,则将具有相同RelatedOrder的所有记录组合在一起并返回记录,返回记录具有最大RecordID的记录。
对不起,如果这没有意义,我很新。
我的班级:
namespace Application
{
class DataRecord
{
public String RecordID { get; set; }
public String RelatedOrder { get; set; }
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("File Path:");
var filePath = Console.ReadLine();
using (var sr = new StreamReader(filePath))
{
var reader = new CsvReader(sr);
IEnumerable<DataRecord> records = reader.GetRecords<DataRecord>();
foreach (var record in records)
{
// If there is a record within the current list of records that shares the same
// RelatedOrder, group all records with the same RelatedOrder and return the record
// with the greatest RecordID
}
Console.ReadKey();
}
}
}
}
答案 0 :(得分:1)
您可以使用LINQ
:
var recordsWithHighestRecordsByRelatedOrder =
reader.GetRecords<DataRecord>()
.GroupBy(dr => dr.RelatedOrder)
.Select(g => g.OrderByDescending(i => i.RecordID).First())
注意:比较和排序字符串时要小心。我想您的意图是以数字方式订购RecordId,而不是按字母顺序排序。最好将您的课程修改为以下内容:
class DataRecord
{
public int RecordID { get; set; } // Make this an int
public String RelatedOrder { get; set; }
}