如何通过组合重复数据显示为单个对象来显示列表对象中的所有数据?

这是我在文件中的输入商店:

50|Carbon|Mercury|M:4;C:40;A:1 90|Oxygen|Mars|M:10;C:20;A:00 90|Serium|Jupiter|M:3;C:16;A:45 85|Hydrogen|Saturn|M:33;C:00;A:3 

这里50,90,90,85表示重量M,C,A表示该元素的每一个中的比例。

现在我想从最高到最低显示每个元素(即碳,氧等),如果有多个具有相同权重的元素,将它们分组在一个权重下按行星 (火星,木星)和元素按字母顺序)按字母顺序排序碳,氧等..)

预期产量

 1)90 Serium;Jupiter (sorted alphabetically by planet name). compounds:M:3;C:16;A:45 Oxygen;Mars compounds:M:10;C:20;A:00 2)85 Hydrogen;Saturn M:33;C:00;A:3 3)50 Carbon;Mercury M:4;C:40;A:1 

这就是我的表现:

 public class Planets { public int Number { get; set; } //This field points to first cell of every row.output 50,90,90,85 public string name { get; set; } //This field points to Second cell of every row.output Hallogen,Oxygen,Hydrogen public string object { get; set; } ////This field points to third cell of every row.output Mercury,Mars,Saturn public List proportion { get; set; } //This will store all proportions with respect to planet object. //for Hallogen it will store 4,40,1.Just store number.ignore M,C,A initials. //for oxygen it will store 10,20,00.Just store number.ignore M,C,A initials. } public class proportion { public int Number { get; set; } } List Planets = new List(); using (StreamReader sr = new StreamReader(args[0])) { String line; while ((line = sr.ReadLine()) != null) { Planets planet = new Planets(); String[] parts = line.Split('|'); planet.Number = Convert.ToInt32(parts[0]); planet.name = parts[1]; planet.obj = parts[2]; String[] smallerParts = parts[3].Split(';'); planet.proportion = new List(); foreach (var item in smallerParts) { proportion prop = new proportion(); prop.Number = Convert.ToInt32(item.Split(':')[1]); planet.proportion.Add(prop); } Planets.Add(planet); } } var data = Planets.OrderByDescending(t => t.Number).ToList();//Highest to lowest. foreach (var item in data) { //What to do for same elements } 

我成功地能够在我的星球列表对象中添加所有4行,如下所示:

  Planets[0]: { Number:50 name: Carbon object:Mercury proportion[0]: { Number:4 }, proportion[1]: { Number:40 }, proportion[2]: { Number:1 } } Etc....... 

这里唯一的问题是显示相同数量的重量预期输出1 )和按行星(火星,木星)按字母顺序排序,然后按元素(碳,氧气等)排序。

我在LinqPad上创建了以下代码以实现您期望的结果,如果您仍需要更改,请告诉我,简单的Linq查询将有助于实现结果:

 void Main() { List customList = Input.Create(); var result = customList.GroupBy(x=>x.Weight,x=>new {x.Element1,x.Element2,x.Detail}) .Select(y=>new { key = y.Key, collection = y.OrderBy(z=>z.Element2) } ).OrderByDescending(h=>h.key); foreach(var n in result) { Console.WriteLine("Weight :: " + n.key); foreach(var g in n.collection) { Console.WriteLine(g.Element1 + ";" + g.Element2); Console.WriteLine("Compounds:" + g.Detail); } } } public class Input { public int Weight {get; set;} public string Element1 {get; set;} public string Element2 {get; set;} public string Detail {get; set;} public Input(int w, string e1, string e2, string d) { Weight = w; Element1 = e1; Element2 = e2; Detail = d; } public static List Create() { List returnList = new List(); returnList.Add(new Input(50,"Carbon","Mercury","M:4;C:40;A:1")); returnList.Add(new Input(90,"Oxygen","Mars","M:10;C:20;A:00")); returnList.Add(new Input(90,"Serium","Jupiter","M:3;C:16;A:45")); returnList.Add(new Input(85,"Hydrogen","Saturn","M:33;C:00;A:3")); return (returnList); } }