[英]Splitting a list<> that each item contains comma separated strings and formatting it out
I have a simple class like this: 我有一个简单的课程,像这样:
class QuickReport
{
public string DeviceName { get; set; }
public string GroupName { get; set; }
public string PinName { get; set; }
public override string ToString()
{
return DeviceName + "," + GroupName + "," + PinName;
}
}
Later I make a list of items with this class: 稍后,我列出此类的项目列表:
List<QuickReport> QR = new List<QuickReport>();
Later in my program it will fill up and when I save it in a text file it will be like this example: 稍后在我的程序中它将填满,当我将其保存在文本文件中时,将类似于以下示例:
HBM\D1,GND,10
HBM\D1,GND,12
HBM\D1,NT_IOp,115
HBM\D1,NT_IOp,117
HBM\D2,GND,8
HBM\D2,NT_IOp,115
HBM\D2,NT_IOp,116
Now I want to make a function to save the text file in more readable manner. 现在,我想要一个函数来以更易读的方式保存文本文件。 That is formatting it by DEVICE, GROUPS and PINS.
即通过DEVICE,GROUPS和PINS对其进行格式化。 So the above example would result in:
因此,上面的示例将导致:
HBM\D1
GND: 10, 12
NT_IOp: 115, 117
HBM\D2
GND: 8
NT_IOp: 115, 116
can you please help and give some ideas? 您能帮忙并提出一些想法吗?
Thanks! 谢谢!
As i understand you have tree structure, where Device have child Groups, and Groups have child pins. 据我了解,您具有树结构,其中设备具有子组,而组具有子销。 You can create custom classes like this:
您可以像这样创建自定义类:
class Group
{
string Name;
//pins that belong to this group
List<string> pins;
}
class Device
{
string Name;
//groups that belong to this device
List<Group> Groups;
}
And than just collect it to List<Device>
and serialize it using XML Serialization . 并且不只是将其收集到
List<Device>
并使用XML Serialization对其进行序列化 。
var query = QR.ToLookup(i=>i.DeviceName, i => new {i.GroupName, i.PinName})
.Select(i=>
new {DeviceName = i.Key,
Groups = i.ToLookup(g=>g.GroupName, g=>g.PinName)});
var sb = new StringBuilder();
foreach ( var device in query)
{
sb.AppendLine(device.DeviceName);
foreach ( var gr in device.Groups)
{
sb.Append(gr.Key + ": ");
sb.Append(String.Join(", ", gr.ToArray()));
sb.AppendLine();
}
sb.AppendLine();
}
var stringToWrite = sb.ToString();
This isn't complete, but it should give you enough to go on. 这还不完整,但是应该可以继续进行。 You'll still need to add your newlines, and remove trailing commas, etc.
您仍然需要添加换行符,并删除结尾的逗号等。
// Make your key the device name
var qrHash = new Dictionary<string, List<QuickReport>>();
// Populate your QR Dictionary here.
var output = new StringBuilder();
foreach (var keyValuePair in qrHash)
{
output.Append(keyValuePair.Key);
var gnd = new StringBuilder("GND: ");
var nt = new StringBuilder("NT_IOp: ");
foreach (var qr in keyValuePair.Value)
{
gnd.Append(qr.GroupName);
nt.Append(qr.PinName);
}
output.Append(gnd);
output.Append(nt);
}
How about using the XmlSerializer to serialize and deserialize your class? 如何使用XmlSerializer序列化和反序列化您的类? This should provide some readable output.
这应该提供一些可读的输出。
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx http://msdn.microsoft.com/zh-CN/library/system.xml.serialization.xmlserializer.aspx
The quickest ways I can think of to do this would either be to loop over the List<> 3 times, eachtime checking on a seperate accessor, writing it out to a StringBuilder, then returning StringBuilder.ToString() from the function. 我能想到的最快方法是遍历List <> 3次,每次检查单独的访问器,将其写出到StringBuilder,然后从函数返回StringBuilder.ToString()。
Or, you could use 3 stringbuilders to hold each accessor type, then push all 3 from the function on return. 或者,您可以使用3个stringbuilder来保存每种访问器类型,然后在返回时从函数中推送所有3种。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.