繁体   English   中英

带SelectMany的String.Format

[英]String.Format with SelectMany

我想在.SelectMany()帮助下在一行中设置一个字符串。 我有一个Object ,其中包含一个Dictionary<String,FileInfo> 对于这些字典中的每个元素,我都希望将密钥放入String.Format()中,但是我总是得到"System.Linq.Enumerable+<SelectManyIterator>d__142[System.String,System.Char]"作为返回值。

我的错在哪里

String.Format(
    "All Strings : {0} on {1} ", 
    MyObject.MyDictionary.Keys.SelectMany(x => x), 
    MyObject.Type);

框架3.5

 String.Format(
        "All Strings : {0} on {1} ", 
        string.Join(string.Empty, MyObject.MyDictionary.Keys.SelectMany(x => x).ToArray()),  
        MyObject.Type);

框架4

 String.Format(
        "All Strings : {0} on {1} ", 
        string.Join(string.Empty, MyObject.MyDictionary.Keys.SelectMany(x => x)),  
        MyObject.Type);

SelectMany将返回IEnumerable,这就是为什么,您需要将键放入字符串中,如下所示:

String.Format("All Strings : {0} on {1}",
     String.Join(", ", MyObject.MyDictionary.Select(x => x.Key)),
     MyObject.Type);

非常感谢所有人,我现在用这种方式解决了这个问题:

String.Format("All Strings : {0} on {1} ", String.Join("; ",MyObject.MyDictionary.Keys), MyObject.Type);

暂无
暂无

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

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