C# 如何通过将重复数据合并为单个对象来显示列表对象中的所有数据?
这是我在文件中的输入存储:C# 如何通过将重复数据合并为单个对象来显示列表对象中的所有数据?,c#,linq,list,sorting,grouping,C#,Linq,List,Sorting,Grouping,这是我在文件中的输入存储: 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表示每个元素的比例 现在我想显示每个元素(即碳、氧等)从最高到最低,如果有多个元素具有相同的权重,则将它们按单个权重分组,并按行星(火星、木
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> 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
}
这里我遇到的唯一问题是,显示相同数字的权重(预期输出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);
}
}
void Main()
{
List customList=Input.Create();
var result=customList.GroupBy(x=>x.Weight,x=>new{x.Element1,x.Element2,x.Detail})
.选择(y=>new{
key=y.key,
collection=y.OrderBy(z=>z.Element2)
}
).OrderByDescending(h=>h.key);
foreach(结果中的变量n)
{
Console.WriteLine(“重量::”+n.key);
foreach(n.collection中的变量g)
{
控制台写入线(g.Element1+“;”+g.Element2);
控制台写入线(“复合物:+g.Detail”);
}
}
}
公共类输入
{
公共整数权重{get;set;}
公共字符串元素1{get;set;}
公共字符串元素2{get;set;}
公共字符串详细信息{get;set;}
公共输入(整数w、字符串e1、字符串e2、字符串d)
{
重量=w;
元素1=e1;
元素2=e2;
细节=d;
}
公共静态列表创建()
{
List returnList=新列表();
添加(新输入(50,“碳”,“汞”,“M:4;C:40;A:1”);
添加(新输入(90,“氧气”,“火星”,“M:10;C:20;A:00”);
添加(新输入(90,“系列”、“木星”、“M:3;C:16;A:45”);
添加(新输入(85,“氢”,“土星”,“M:33;C:00;A:3”);
返回(返回列表);
}
}
这是关于显示还是关于存储数据?因此,具有相同重量的行星应该一起储存在一个物体中,还是应该以正确的顺序显示任意数量的行星?如果是后者,那么您目前的显示方法是什么样子的,请提供代码,我将研究如何解决您的显示顺序问题。@meansomerandoms:这只是关于显示数据而不是存储数据。具有相同重量的行星应合并为一个,以显示火星和木星的重量相同,因此我需要将它们显示在一个行星上,而不是不同的行星上。您想这样做吗将所有具有相同权重的行星封装到另一个数据结构中,以便您知道在该数据结构中,所有具有相同权重的行星都是按正确顺序排列的。或者,在显示完整的行星列表时,是否要执行排序逻辑。我要问的是,具有相同重量的行星是否应该存储在一起,这样你就可以以正确的顺序显示它们,或者,只要显示逻辑正确地排列它们,它们如何存储无关紧要。两者都是可能的。一个简单的重量分组将为您完成这项工作,您是否尝试过使用它MyBe“ThenBy”可以帮助您在主属性之后按次属性排序,从而获得正确的顺序。示例代码是:exampleList.OrderBy(item=>item.PrimaryProperty)。然后是By(item=>item.SecondaryProperty)。ToList();Dump方法不存在Dump方法在linq中不可用。编辑的Dump是LinqPad方法,因此在这里不起作用,您可以使用foreach循环打印并打印所有相关元素,最终实体将有两个元素,键和集合,其中集合本身是一个IOrderedEnumerable,它必须是foreach内部的foreach才能打印所有细节。我已经添加了一个要打印的代码,虽然这将再次在单独的行中显示所有内容,但如果您希望避免重印权重,则需要更多的逻辑,使用权重对其进行分组。本质上,内部foreach循环需要验证weight的值,如果相同,则避免打印是的,我使用了您的代码,但weight 90仍在打印两次。那么如何避免这种情况呢?现在检查它修改代码以准确实现您需要的结果。如果可以,请将此标记为答案
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);
}
}