Having dictionary where key can be anything (e.g. int) and values is some text I want to output.
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.");
To get output:
string lResult = dict. ... //there should go the LINQ query
Console.WriteLine(lResult);
Output:
This is the first line.
This is the second line.
This is the third line.
Q: Is it possible to concatenate the lines from Dictionary
to have them as one string without using external variable?
I tried approach using some Select/SelectMany/Zip
solutions, but I cannot figure how to pass the value from 1 LINQ call to other without using external variable.
Another idea is to Select
values, put them to the List and then Concatenate (again using external variable). Like:
string tmp = "";
dict.Select(a => a.Value).ToList().ForEach(b => tmp += b);
You shouldn't use LINQ to concat strings. This can become very expenisve. Use string.Join()
innstead:
string result = string.Join(Environment.NewLine, dict.Values);
However, this does not guarantee the correct order, because a Dictionary<>
is not sorted. To sort the output by the Key
s you can do this:
string sorted = string.Join(Environment.NewLine,
dict.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value));
You can do:
string.Join(Environment.NewLine, dict.Values)
Note however, that the documentation states the values will be retrieved in an unspecified order.
I suggest using StringBuilder if you want to use LINQ. Otherwise the performance will suffer too much:
string lResult = dict.Values.Aggregate(new StringBuilder(), (a, b) => a.Append(b)).ToString()
Appending string
in a loop
ForEach(b => tmp += b)
is an antipattern; you should use StringBuilder
. In case you have to use Linq
(not string.Join
which is specially designed for it):
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With