[英]Format/ group list of strings by condition
I have a question regarding formatting List of strings. 我对格式化字符串列表有疑问。 I'm looping through dictionary of two strings: value and tagname.
我正在遍历两个字符串的字典:值和标记名。
myList.Add(myDict.Keys.ElementAt(n) + ":" + myDict.Values.ElementAt(n).Value);
When the iteration is done, I need to format that string as follows: 迭代完成后,我需要按如下所示格式化该字符串:
for (int tmp = 0; tmp < myList.Count; tmp++)
{
if (tmp < myList.Count - 1)
{
tempString += String.Format(myList[tmp].ToString() + " ,");
}
}
Output of that tempstring will be like this: TagName: Value, TagName: Value, TagName: Value. 该临时字符串的输出将如下所示:TagName:值,TagName:值,TagName:值。 But here's the tricky part (for me): several tag names are the same, and for the program purposes, these items which have same tag name needs to be grouped by that tag name, something like this: TagName: value, TagName: value, value, value Do you have any suggestions for this?
但是,这是棘手的部分(对我而言):几个标签名称相同,并且出于程序目的,具有相同标签名称的这些项目需要按该标签名称分组,如下所示:TagName:value,TagName:value ,价值,价值您对此有何建议?
Update (reading XML): 更新(读取XML):
var evt = (from el in doc.Descendants("test")
where el.Parent.Name == "Event_1"
group el by el.Parent.Element("NameOfEvent").Value into g
select new {
Name = g.Key,
Tests = g.Select(x => new {
Value = x.Element("value").Value,
TagName = x.Element("tagName").Value
})
}).FirstOrDefault();
Console.WriteLine("Event name: " + evt.Name);
foreach (var test in evt.Tests)
{
}
It looks like you need a combination of LINQ's Group By and String.Join . 看起来您需要LINQ的Group By和String.Join的组合。
Group By takes a list of objects and produces a list of lists of objects where each list's objects all contain something in common. 分组依据可获取对象列表,并生成对象列表列表,其中每个列表的对象都包含一些共同点。
{"Key1", "Value1"},
{"Key2", "Value2"},
{"Key1", "Value3"}
Becomes 变
{
{"Key1", "Value1"},
{"Key1", "Value3"}
},
{
{"Key2", "Value2"},
}
When you group all of the values by their values. 将所有值按它们的值分组时。 Then you can just take that result and join the values in a comma separated list using String.Join .
然后,您可以获取该结果,然后使用String.Join将值连接到以逗号分隔的列表中。
What you want to do is construct a list that contains all of your values grouped together under the same key and then join them into a string. 您要做的是构造一个列表,其中包含所有在同一键下分组在一起的所有值,然后将它们连接到字符串中。
There are 3 steps to do this: 有3个步骤可以做到这一点:
Which would look something like this: 看起来像这样:
// Your keys and values
List<KeyValuePair<string, string>> dict = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("TagName1", "value1"),
new KeyValuePair<string, string>("TagName2", "value2"),
new KeyValuePair<string, string>("TagName1", "value3")
};
// 1. Group all of the values together based on the key
IEnumerable<IGrouping<string, KeyValuePair<string, string>>> groupedValues =
dict.GroupBy(v => v.Key);
// 2. Construct a string for each of the groups
IEnumerable<string> builtValues = groupedValues.Select(group =>
{
// returns "TagName1:value1, value2" etc.
return group.Key + ":" + string.Join(", ", group.Select(g => g.Value));
});
// 3. Construct the final string from each of the constructed strings
string finalResult = string.Join(", ", builtValues);
Then finalResult
would contain TagName1:value1, value3, TagName:value2
, which seems to be relatively what you want. 然后
finalResult
将包含TagName1:value1, value3, TagName:value2
,这似乎是相对你想要什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.