使用没有外部变量的 LINQ 连接字典(值)中的所有字符串
Concat all strings from dictionary (values) using LINQ without external variable
有字典,其中键可以是任何东西(例如 int),值是我想要输出的一些文本。
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "This is the first line.");
dict.Add(2, "This is the second line.");
dict.Add(3, "This is the third line.");
获取输出:
string lResult = dict. ... //there should go the LINQ query
Console.WriteLine(lResult);
输出:
This is the first line.
This is the second line.
This is the third line.
问:是否可以在不使用外部变量的情况下将 Dictionary
中的行连接成一个字符串?
我尝试使用一些 Select/SelectMany/Zip
解决方案,但我不知道如何在不使用外部变量的情况下将值从 1 个 LINQ 调用传递给其他调用。
另一个想法是 Select
值,将它们放入列表,然后连接(再次使用外部变量)。喜欢:
string tmp = "";
dict.Select(a => a.Value).ToList().ForEach(b => tmp += b);
你可以这样做:
string.Join(Environment.NewLine, dict.Values)
但是请注意,the documentation 表示将以未指定的顺序检索值。
您不应使用 LINQ 来连接字符串。这可以变成very expenisve. Use string.Join()
instead:
string result = string.Join(Environment.NewLine, dict.Values);
但是,这并不能保证顺序正确,因为Dictionary<>
没有排序。要按 Key
s 对输出进行排序,您可以这样做:
string sorted = string.Join(Environment.NewLine,
dict.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value));
如果您想使用 LINQ,我建议您使用 StringBuilder。否则性能会受到太大影响:
string lResult = dict.Values.Aggregate(new StringBuilder(), (a, b) => a.Append(b)).ToString()
在 循环中追加 string
ForEach(b => tmp += b)
是一个反模式;你应该使用 StringBuilder
。如果你必须使用 Linq
(不是专门为它设计的string.Join
):
dict.Add(1, "This is the first line.");
dict.Add(2, "This is the second line.");
dict.Add(3, "This is the third line.");
string result = dict
.OrderBy(pair => pair.Key)
.Aggregate((StringBuilder) null,
(sb, pair) => (sb == null
? new StringBuilder()
: sb.Append(Environment.NewLine)).Append(pair.Value))
.ToString();
有字典,其中键可以是任何东西(例如 int),值是我想要输出的一些文本。
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "This is the first line.");
dict.Add(2, "This is the second line.");
dict.Add(3, "This is the third line.");
获取输出:
string lResult = dict. ... //there should go the LINQ query
Console.WriteLine(lResult);
输出:
This is the first line.
This is the second line.
This is the third line.
问:是否可以在不使用外部变量的情况下将 Dictionary
中的行连接成一个字符串?
我尝试使用一些 Select/SelectMany/Zip
解决方案,但我不知道如何在不使用外部变量的情况下将值从 1 个 LINQ 调用传递给其他调用。
另一个想法是 Select
值,将它们放入列表,然后连接(再次使用外部变量)。喜欢:
string tmp = "";
dict.Select(a => a.Value).ToList().ForEach(b => tmp += b);
你可以这样做:
string.Join(Environment.NewLine, dict.Values)
但是请注意,the documentation 表示将以未指定的顺序检索值。
您不应使用 LINQ 来连接字符串。这可以变成very expenisve. Use string.Join()
instead:
string result = string.Join(Environment.NewLine, dict.Values);
但是,这并不能保证顺序正确,因为Dictionary<>
没有排序。要按 Key
s 对输出进行排序,您可以这样做:
string sorted = string.Join(Environment.NewLine,
dict.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value));
如果您想使用 LINQ,我建议您使用 StringBuilder。否则性能会受到太大影响:
string lResult = dict.Values.Aggregate(new StringBuilder(), (a, b) => a.Append(b)).ToString()
在 循环中追加 string
ForEach(b => tmp += b)
是一个反模式;你应该使用 StringBuilder
。如果你必须使用 Linq
(不是专门为它设计的string.Join
):
dict.Add(1, "This is the first line.");
dict.Add(2, "This is the second line.");
dict.Add(3, "This is the third line.");
string result = dict
.OrderBy(pair => pair.Key)
.Aggregate((StringBuilder) null,
(sb, pair) => (sb == null
? new StringBuilder()
: sb.Append(Environment.NewLine)).Append(pair.Value))
.ToString();