C#在CSV文件中获取重复条目,并通过取平均值删除重复条目

C#在CSV文件中获取重复条目,并通过取平均值删除重复条目,c#,list,csv,C#,List,Csv,我的程序创建了一个.csv文件,文件旁边有一个人名和一个整数 文件中偶尔会有两个同名的条目,但时间不同。我只想要每个人的一个例子 我想取两个数字的平均值,只为名称生成一行,其中的数字将是两个现有数字的平均值 亚历克斯·皮特有两个数字。我如何取105和71的平均值(在本例中)来生成一行,其中只包括88岁的Alex Pitt 这里是我如何创建我的CSV文件,如果需要引用 public void CreateCsvFile() { PaceCalculator ListGat

我的程序创建了一个.csv文件,文件旁边有一个人名和一个整数

文件中偶尔会有两个同名的条目,但时间不同。我只想要每个人的一个例子

我想取两个数字的平均值,只为名称生成一行,其中的数字将是两个现有数字的平均值

亚历克斯·皮特有两个数字。我如何取105和71的平均值(在本例中)来生成一行,其中只包括88岁的Alex Pitt

这里是我如何创建我的CSV文件,如果需要引用

public void CreateCsvFile()
    {
        PaceCalculator ListGather = new PaceCalculator();
        List<string> NList = ListGather.NameGain();
        List<int> PList = ListGather.PaceGain();

        List<string> nAndPList = NList.Zip(PList, (a, b) => a + ", " + b).ToList();

        string filepath = @"F:\A2 Computing\C# Programming Project\ScheduleFile.csv";

        using (var file = File.CreateText(filepath))
        {
            foreach (var arr in nAndPList)
            {
                if (arr == null || arr.Length == 0) continue;
                file.Write(arr[0]);
                for (int i = 1; i < arr.Length; i++)
                {
                    file.Write(arr[i]);
                }
                file.WriteLine();
            }
        }
    }
public void CreateCsvFile()
{
PaceCalculator ListGather=新的PaceCalculator();
List NList=ListGather.NameGain();
List PList=listgage.PaceGain();
List nAndPList=NList.Zip(PList,(a,b)=>a+“,”+b.ToList();
字符串文件路径=@“F:\A2计算\C#编程项目\ScheduleFile.csv”;
使用(var file=file.CreateText(filepath))
{
foreach(nAndPList中的var arr)
{
如果(arr==null | | arr.Length==0)继续;
file.Write(arr[0]);
对于(int i=1;i
更改以下代码:

 List<string> nAndPList = NList.Zip(PList, (a, b) => a + ", " + b).ToList();
List nAndPList=NList.Zip(PList,(a,b)=>a+,“+b.ToList();

List nAndPList=NList.Zip(PList,(a,b)=>a+,“+b)
托利斯先生()
.GroupBy(x=>x.[要按其分组的字段])
.选择(y=>y.First);

首先,您可以编写当前的
CreateCsvFile
更简单,如下所示:

public void CreateCsvFile()
{
    var filepath = @"F:\A2 Computing\C# Programming Project\ScheduleFile.csv";
    var ListGather = new PaceCalculator();

    var records =
        ListGather.NameGain()
            .Zip(ListGather.PaceGain(),
                (a, b) => String.Format("{0},{1}", a, b));

    File.WriteAllLines(filepath, records);
}
public void CreateCsvFile()
{
    var filepath = @"F:\A2 Computing\C# Programming Project\ScheduleFile.csv";
    var ListGather = new PaceCalculator();

    var records =
        from record in ListGather.NameGain()
            .Zip(ListGather.PaceGain(),
                (a, b) => new { Name = a, Pace = b })
        group record.Pace by record.Name into grs
        select String.Format("{0},{1}", grs.Key, grs.Average());

    File.WriteAllLines(filepath, records);
}
现在,如果您有重复的名称,可以很容易地更改它以计算平均速度,如下所示:

public void CreateCsvFile()
{
    var filepath = @"F:\A2 Computing\C# Programming Project\ScheduleFile.csv";
    var ListGather = new PaceCalculator();

    var records =
        ListGather.NameGain()
            .Zip(ListGather.PaceGain(),
                (a, b) => String.Format("{0},{1}", a, b));

    File.WriteAllLines(filepath, records);
}
public void CreateCsvFile()
{
    var filepath = @"F:\A2 Computing\C# Programming Project\ScheduleFile.csv";
    var ListGather = new PaceCalculator();

    var records =
        from record in ListGather.NameGain()
            .Zip(ListGather.PaceGain(),
                (a, b) => new { Name = a, Pace = b })
        group record.Pace by record.Name into grs
        select String.Format("{0},{1}", grs.Key, grs.Average());

    File.WriteAllLines(filepath, records);
}

我建议在将所有内容放入CSV文件之前合并重复项

使用:


我希望你能理解我想说的话。但是,我建议为您的人员使用唯一标识符。迟早会有两个人同名出现(就像在一家大公司里)

当你在写CSV时,你可以写任何你喜欢的东西。只要写你想要的数据。我已经在做了,我只想删除有重复项的行。@Dan-o试图说的是,在我合并两个列表之前,你可以在写CSVso之前删除重复项,那么我应该删除或合并重复项吗?@geogeboulton-不,你必须先合并(而不是压缩)两个列表,但不是作为字符串,然后计算平均值,然后写出记录;约翰,12岁;首先,我在你提供的列表中没有看到John,其次,这并不是不正确的……这非常有效。因此,这里您将对找到平均值的速度进行分组?@geogeboulton-严格来说,分组只是创建一个与每个名称相关联的值列表。调用
.Average()
可以通过计算平均值将整数列表减少到一个双精度值。
//Something like this:
Make a foreach loop for every entry in your duplicateChecker => 

Use Distrinc again on duplicateChecker to make sure you won't go twice through the same duplicate =>

Get the Value of the current String and search it in Nlist =>

Get the Index of the current Element in Nlist and search for the Index in Plist =>

Get the Integer of Plist and store it in a array =>

// make sure your math method runs before a new name starts. After that store the new values in your nAndPList
Once the Loop is through with the first name use a math method.