Search code examples
c#linqnamevaluecollection

How to get all values as a single string from NameValueCollection?


How to key all values from NameValueCollection as a single string,
Now I am using following method to get it:

public static string GetAllReasons(NameValueCollection valueCollection)
{
    string _allValues = string.Empty;

    foreach (var key in valueCollection.AllKeys)
        _allValues += valueCollection.GetValues(key)[0] + System.Environment.NewLine;

    return _allValues.TrimEnd(System.Environment.NewLine.ToCharArray());
}

Any simple solution using Linq?


Solution

  • You could use the following:

    string allValues = string.Join(System.Environment.NewLine, valueCollection.AllKeys.Select(key => valueCollection[key]));