添加字段时的C#逻辑问题

我有一个包含 4 条记录的列表。如果有相同年龄的动物,我需要添加它们的年龄并将其添加为单独的列。例如看下表。

姓名 | 年龄 | 性别

蚂蚁 | 9 | 米

蚂蚁 | 2 | 米

狮子| 8 | 米

猪 | 1 | F

有 2 条记录是重复的 (Ant)。我需要添加重复项的年龄,因此会有一条新记录,例如:

蚂蚁 | 11 | 米

总共应该有 5 条记录以及新添加的行(在上面添加)。

我面临的问题是,我能够添加新添加的记录(即通过添加年龄字段)。但是,此记录会覆盖初始字段中的第一条记录。如何将此记录添加为新字段。

到目前为止我的代码:

foreach (var animal in duplicates)

{

        Animal ani = listAnimals.First(a=> a.Name.Equals(animal.Name));


        ani.Age += animal.Age;

}


ibeautiful
浏览 117回答 3
3回答

有只小跳蛙

你可以很容易地使用 Linq 做到这一点假设您的 Animal Type 声明为public class Animal{public string Name {get;set;}public int Age{get;set;}public string Gender {get;set;}}您可以按名称对重复项进行分组并计算年龄总和。var list = new List<Animal>{&nbsp;&nbsp; &nbsp; new Animal{Name="Ant",Age=9,Gender="M"},&nbsp; &nbsp; new Animal{Name="Ant",Age=2,Gender="M"},&nbsp; &nbsp; new Animal{Name="Lion",Age=8,Gender="M"},&nbsp; &nbsp; new Animal{Name="Pig",Age=1,Gender="F"},&nbsp; &nbsp; };&nbsp; &nbsp; list.AddRange( list.GroupBy(x=>x.Name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(x=>x.Count()>1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(x=> new Animal{Name = x.First().Name, Gender= x.First().Gender, Age = x.Sum(c=>c.Age)}));输出Name Age GenderAnt&nbsp; &nbsp;9&nbsp; &nbsp;M&nbsp;Ant&nbsp; &nbsp;2&nbsp; &nbsp;M&nbsp;Lion&nbsp; 8&nbsp; &nbsp;M&nbsp;Pig&nbsp; &nbsp;1&nbsp; &nbsp;F&nbsp;Ant&nbsp; &nbsp;11&nbsp; M&nbsp;

大话西游666

var animals = new List<Animal>{&nbsp; &nbsp; new Animal { Name = "Ant", Age = 9, Gender = "M"},&nbsp; &nbsp; new Animal { Name = "Ant", Age = 2, Gender = "M"},&nbsp; &nbsp; new Animal { Name = "Lion", Age = 8, Gender = "M"},&nbsp; &nbsp; new Animal { Name = "PIG", Age = 1, Gender = "F"},};var new_animals =&nbsp; &nbsp; from animal in animals&nbsp; &nbsp; group animal by animal.Name into grp&nbsp; &nbsp; where grp.Count() >= 2&nbsp; &nbsp; select new Animal&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Name = grp.Key,&nbsp; &nbsp; &nbsp; &nbsp; Age = grp.Sum(a => a.Age),&nbsp; &nbsp; &nbsp; &nbsp; Gender = "M"&nbsp; &nbsp; };animals.AddRange(new_animals);foreach(var animal in animals){&nbsp; &nbsp; WriteLine(animal);}

holdtom

您的代码会覆盖现有的动物,因为您没有为总数创建新的动物new Animal()。在重复项下方的正确位置获取重复项总计非常棘手。为此,我将创建一个新类,Animal以便能够将动物与总数记录区分开来。class Animal{&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; public int Age { get; set; }&nbsp; &nbsp; public string Gender { get; set; }}class AnimalTotals : Animal{}像这样创建重复的总计:var duplicateTotals = listAnimals&nbsp; &nbsp; .GroupBy(a => (a.Name, a.Gender))&nbsp; &nbsp; .Where(g => g.Count() > 1)&nbsp; &nbsp; .Select(g => new AnimalTotals {&nbsp; &nbsp; &nbsp; &nbsp; Name = g.Key.Name,&nbsp; &nbsp; &nbsp; &nbsp; Age = g.Sum(a => a.Age),&nbsp; &nbsp; &nbsp; &nbsp; Gender = g.Key.Gender&nbsp; &nbsp; });请注意,我使用值元组(在 C# 7.0 中引入)来创建由名称和性别组成的组键。然后我将重复的总数与现有列表合并,并对结果进行排序以获得重复项下方的总数。listAnimals = listAnimals&nbsp; &nbsp; .Concat(duplicateTotals)&nbsp; &nbsp; .OrderBy(a => a.Name)&nbsp; &nbsp; .ThenBy(a => a.Gender)&nbsp; &nbsp; .ThenBy(a => a is AnimalTotals ? 1 : 0)&nbsp; &nbsp; .ToList();有了这个样本数据var listAnimals = new List<Animal> {&nbsp; &nbsp; new Animal{ Name="Ant",&nbsp; Age = 9, Gender = "M" },&nbsp; &nbsp; new Animal{ Name="Ant",&nbsp; Age = 2, Gender = "M" },&nbsp; &nbsp; new Animal{ Name="Ant",&nbsp; Age = 5, Gender = "F" },&nbsp; &nbsp; new Animal{ Name="Ant",&nbsp; Age = 1, Gender = "F" },&nbsp; &nbsp; new Animal{ Name="Lion", Age = 8, Gender = "M" },&nbsp; &nbsp; new Animal{ Name="Pig",&nbsp; Age = 1, Gender = "F" },};的输出foreach (Animal a in listAnimals) {&nbsp; &nbsp; if (a is AnimalTotals) {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"{a.Name} {a.Gender} totals: {a.Age,4}");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"{a.Name,-6}&nbsp; &nbsp;{a.Age,2}&nbsp; &nbsp;{a.Gender}");&nbsp; &nbsp; }}是Ant&nbsp; &nbsp; &nbsp; &nbsp;5&nbsp; &nbsp;FAnt&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp;FAnt F totals:&nbsp; &nbsp; 6Ant&nbsp; &nbsp; &nbsp; &nbsp;9&nbsp; &nbsp;MAnt&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; &nbsp;MAnt M totals:&nbsp; &nbsp;11Lion&nbsp; &nbsp; &nbsp; 8&nbsp; &nbsp;MPig&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp;F
打开App,查看更多内容
随时随地看视频慕课网APP