如何通过组合重复数据显示为单个对象来显示列表对象中的所有数据
本文关键字:数据 对象 显示 列表 组合 何通过 单个 | 更新日期: 2023-09-27 18:07:52
这是我在文件中的输入存储:
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表示每个元素的比例
现在我想显示每个元素(i。e碳,氧等)从最高到最低,如果有多个元素具有相同的权重,将它们分组在一个单一的权重下,并按字母顺序排列行星(火星,木星),然后元素(碳,氧等)
预期输出:
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> 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> Planets = new List<Planets>();
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<proportion>();
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<Input> 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<Input> Create()
{
List<Input> returnList = new List<Input>();
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);
}
}