C# 循环浏览列表并合并标识符相同的总计

C# 循环浏览列表并合并标识符相同的总计,c#,loops,foreach,C#,Loops,Foreach,我有一份目标车的清单 其中的变量包括: 制造 模型 服务成本 假设我在列表中填写了: 法拉利,F50300 保时捷,911,700 丰田,凯美瑞,300 保时捷,911,400 宝马,Z41200 保时捷,911,900 保时捷,356A,700 如您所见,我的列表中包含三项保时捷911的维修费用记录 我如何循环浏览我的列表,找到重复的911,并将它们组合成一个记录?因此,我最终得出以下结论: 法拉利,F50300 保时捷,911,2000 丰田,凯美瑞,300 宝马,Z41200 保时捷,35

我有一份目标车的清单 其中的变量包括:

制造

模型

服务成本

假设我在列表中填写了:

法拉利,F50300

保时捷,911,700

丰田,凯美瑞,300

保时捷,911,400

宝马,Z41200

保时捷,911,900

保时捷,356A,700

如您所见,我的列表中包含三项保时捷911的维修费用记录

我如何循环浏览我的列表,找到重复的911,并将它们组合成一个记录?因此,我最终得出以下结论:

法拉利,F50300

保时捷,911,2000

丰田,凯美瑞,300

宝马,Z41200

保时捷,356A,700

到目前为止,我所做的一切都行不通,因为我的记录可能会在错误的领域结束:

    List<Car> CombinedCarRecords = new List<Car>(CarDetailRecords); //Original list here being used
    List<Car> NormalList = new List<Car>();
    List<Car> NewList = new List<Car>();//Making new lists to keep the records in
    Car normalRecord = new Car();
    Car combinedRecord = new Car();//new objects to keep the values in and add the others
    string oldVal = "";
    string newVal = "";//used to find the same cars
    foreach (var item in CombinedCarRecords )
    {
        normalRecord = new ClaimDetailRecord();
        combinedRecord = new ClaimDetailRecord();
        oldVal = newVal;
        newVal = item.Model;
        if (oldVal == newVal)
        {
            combinedRecord = item;
            CombinedCarRecords.Add(combinedRecord);
        }
        else
        {
            normalRecord = item;
            NormalList.Add(normalRecord);
        }
    }//I think the way I try to find the records here is not working, as the old and new values will always be different, if maybe not for some records where they are right after each other. But there is still that initial one

    decimal totalP = 0;

    if (CombinedTariffsRecords.Count > 0)
    {
        foreach (var item in CombinedTariffsRecords)
        {
        }
    }
    else
        NewList = NormalList;
    //This is where I'm supposed to add up the totals, but I think my code before will render this code useless
List CombinedCarRecords=新列表(CarDetailRecords)//这里使用的是原始列表
List NormalList=新列表();
List NewList=新列表()//制作新的列表以保存记录
汽车正常记录=新车();
汽车组合记录=新车()//用于保留值并添加其他值的新对象
字符串oldVal=“”;
字符串newVal=“”//以前也找过同样的车
foreach(组合carrecords中的var项)
{
normalRecord=新的ClaimDetailRecord();
combinedRecord=新的ClaimDetailRecord();
oldVal=newVal;
newVal=item.Model;
if(oldVal==newVal)
{
合并记录=项目;
CombinedCarRecords.Add(combinedRecord);
}
其他的
{
正常记录=项目;
NormalList.Add(normalRecord);
}
}//我认为我试图在这里找到记录的方式是不起作用的,因为旧的和新的值总是不同的,如果不是对于某些记录,它们是紧跟在一起的话。但还是有最初的一个
十进制totalP=0;
如果(合并的TariffsRecords.Count>0)
{
foreach(组合汇率记录中的var项目)
{
}
}
其他的
NewList=NormalList;
//这是我应该把总数加起来的地方,但我认为我以前的代码会使这段代码变得无用

总之,我已经尝试过了,但我想不出更好的方法来存储值并合并我的记录。

最简单的方法是使用LINQ的
可枚举。GroupBy
Sum

var newCarList = NormalList
   .GroupBy(c => new {  c.Make, c.Model })
   .Select(carGroup => new Car{ 
       Make = carGroup.Key.Make, 
       Model = carGroup.Key.Model,
       ServiceCost = carGroup.Sum(c => c.ServiceCost)
    })
   .ToList();

最简单的方法是使用LINQ的
可枚举。GroupBy
Sum

var newCarList = NormalList
   .GroupBy(c => new {  c.Make, c.Model })
   .Select(carGroup => new Car{ 
       Make = carGroup.Key.Make, 
       Model = carGroup.Key.Model,
       ServiceCost = carGroup.Sum(c => c.ServiceCost)
    })
   .ToList();
void Main()
{
var cars=新列表
{ 
新车{Make=“法拉利”,Model=“F50”,维修成本=1000},
新车{Make=“Ferrari”,Model=“F50”,维修成本=2000},
新车{Make=“Porsche”,Model=“911”,维修成本=2000}
};
var group=cars.GroupBy(car=>car.Make+car.Model);
foreach(分组中的变量g)
{
WriteLine(“{0}{1}:{2}”,g.Element.Make,g.Element.Model,g.Costs);
}
}
班车
{
公共字符串模型{get;set;}
公共字符串Make{get;set;}
公共十进制服务成本{get;set;}
}
void Main()
{
var cars=新列表
{ 
新车{Make=“法拉利”,Model=“F50”,维修成本=1000},
新车{Make=“Ferrari”,Model=“F50”,维修成本=2000},
新车{Make=“Porsche”,Model=“911”,维修成本=2000}
};
var group=cars.GroupBy(car=>car.Make+car.Model);
foreach(分组中的变量g)
{
WriteLine(“{0}{1}:{2}”,g.Element.Make,g.Element.Model,g.Costs);
}
}
班车
{
公共字符串模型{get;set;}
公共字符串Make{get;set;}
公共十进制服务成本{get;set;}
}

查看GroupBy()扩展方法。在列表中循环时,您正在创建一个重复记录列表,但如果某个项已经存在,则不检查该列表。它非常简单:循环遍历所有项目,如果不在列表中,则克隆对象并放入另一个列表(列表N),否则将服务成本添加到现有(列表N)行中。查看GroupBy()扩展方法。循环遍历列表时,您创建的是重复记录列表,但如果某个项目已经存在,则不检查该列表。很简单:循环浏览所有项目,如果不在列表中,克隆对象并放入另一个列表(列表N),否则将服务成本添加到现有列表(列表N)行。添加两个值以生成键可能会导致冲突并生成一个或多个临时字符串添加两个值以生成键可能会导致冲突并生成一个或多个临时字符串