LINQ approach to flatten Dictionary to string I have a Dictionary&lt;string,string&gt; and want to flatten it out with this pattern:

{key}={value}|{key}={value}|{key}={value}|


I tried with a LINQ approach at first but couldn't solve it, so I ended up writing an extension method like this:

public static string ToString(this Dictionary&lt;string,string&gt; source, string keyValueSeparator, string sequenceSeparator)
{
  if (source == null)
    throw new ArgumentException("Parameter source can not be null.");

  var str = new StringBuilder();
  foreach (var keyvaluepair in source)
    str.Append(string.Format("{0}{1}{2}{3}", keyvaluepair.Key, keyValueSeparator, keyvaluepair.Value, sequenceSeparator));
  var retval = str.ToString();
  return retval.Substring(0,retval.Length - sequenceSeparator.Length); //remove last  seq_separator
}


Is it possible to solve this with LINQ?
Something like this should work:

public static string ToString(this Dictionary&lt;string,string&gt; source, string keyValueSeparator, string sequenceSeparator)
{
  if (source == null)
    throw new ArgumentException("Parameter source can not be null.");

  var pairs = source.Select(x =&gt; string.Format("{0}{1}{2}", x.Key, keyValueSeparator, x.Value));

  return string.Join(sequenceSeparator, pairs);
}