简体   繁体   English

如何转换字典 <string, string> 在LINQ中赋予字符串属性?

[英]How to convert Dictionary<string, string> to attribute string in LINQ?

I have a collection of key/values in the form of a Dictionary<string, string> . 我有一个Dictionary<string, string>形式的键/值集合。

How would you convert or aggregate this into an attribute string: 您如何将其转换或聚合为属性字符串:

key1="value1" key2="value2" key3="value3"

I believe this can be achieved using Aggregate function, however, I find the documentation on this particular extension method confusing. 我相信可以使用Aggregate函数来实现,但是,我发现有关此特定扩展方法的文档令人困惑。 Any help is much appreciated. 任何帮助深表感谢。

I'd use: 我会用:

.NET 4: .NET 4:

var text = string.Join(" ", 
              dictionary.Select(pair => string.Format("{0}={1}",
                                pair.Key, pair.Value));

.NET 3.5 (where string.Join has fewer overloads) .NET 3.5(其中string.Join具有较少的重载)

var text = string.Join(" ", 
              dictionary.Select(pair => string.Format("{0}={1}",
                                pair.Key, pair.Value)
                        .ToArray());

If you need to do any escaping, do it in the string.Format call. 如果需要进行任何转义,请在string.Format调用中进行。 Of course, you don't need to use string.Format - you could use: 当然,您不需要使用string.Format您可以使用:

var text = string.Join(" ", dict.Select(pair => pair.Key + "=" + pair.Value));

It depends on which you find more readable. 这取决于您发现更具可读性。 (The performance difference will be negligible.) (性能差异可以忽略不计。)

var dict = new Dictionary<string, string>
                {
                    {"1", "first"},
                    {"2", "second"}
                };

var result = dict.Aggregate(new StringBuilder(),
                            (sb, kvp) => sb.AppendFormat("{0}=\"{1}\" ", kvp.Key, kvp.Value),
                            sb => sb.ToString());
string.Join(" ", myDict.Select(d => string.Format("{0} = {1}", d.Key, d.Value)))

So you want the keys and values of the Dictionary output as a string in this manner? 因此,您是否希望以这种方式将Dictionary的键和值输出为字符串? Sounds easy enough: 听起来很简单:

var myDictionary = new Dictionary<string,string>();
//populate Dictionary

//a Dictionary<string,string> is an IEnumerable<KeyValuePair<sring,string>>
//so, a little Linq magic will work wonders
var myAttributeString = myDictionary.Aggregate(new StringBuilder(), (s, kvp) => s.Append(kvp.Key + "=\"" + (kvp.Value ?? String.Empty) + "\" "));

The result will be a string like the one in your question, with a trailing space (which in XML isn't a huge deal, but you can trim it if you want). 结果将是一个与您的问题类似的字符串,并带有一个尾随空格(在XML中这没什么大不了的,但是您可以根据需要对其进行裁剪)。

You could also use .NET's XML features to actually put the values into XML as an attribute string of an element. 您还可以使用.NET的XML功能,将值实际作为元素的属性字符串放入XML中。 The attribute string will end up in an XML element in a document, which is probably where it needs to go anyway: 该属性字符串将最终出现在文档中的XML元素中,无论如何它可能都需要放在其中:

XMLDocument doc = new XMLDocument()
XMLElement myElement = doc.CreateElement("myElement")

foreach(var kvp in myDictionary)
   myElement.SetAttribute(kvp.Key, kvp.Value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM