Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:41:19+00:00 2026-06-15T07:41:19+00:00

I have a NameValueCollection which I need to convert to a Map and I

  • 0

I have a NameValueCollection which I need to convert to a Map and I just can’t work it out. I tried:

let headerMap (m : MailMessage) = m.Headers |> Map.map (fun k v -> v.[k])

Do I need to use Seq.map instead?

Basically the point of this is that I want to serialize the headers in a System.Net.MailMessage to JSON.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:41:20+00:00Added an answer on June 15, 2026 at 7:41 am

    Daniel’s answer will work just fine, but I thought I’d offer some additional alternatives:

    Array.fold — This should be faster than Daniel’s version since it avoids the overhead of the iterators.

    let mapOfNameValueCollection (collection : NameValueCollection) =
        (Map.empty, collection.AllKeys)
        ||> Array.fold (fun map key ->
            let value = collection.[key]
            Map.add key value map)
    

    Array.fold with sets of values — Similar to the code above, but returns the value as a Set<string> which may be useful if you want to determine if some value is in the returned set of values.

    let mapOfNameValueCollection (collection : NameValueCollection) =
        (Map.empty, collection.AllKeys)
        ||> Array.fold (fun map key ->
            let valueSet =
                match collection.[key] with
                | null ->
                    Set.empty
                | values ->
                    Set.ofArray <| values.Split [| ',' |]
            Map.add key valueSet map)
    

    Recursive loop — Creates the map item-by-item with a recursive loop. I wouldn’t use this in practice because the Array.fold version would be easier and faster. However, this approach could be faster if the specific collection class you’re using (derived from NameValueCollection) overrides the AllKeys property and has some weird internal behavior which takes a long time to return the property value.

    let mapOfNameValueCollection (collection : NameValueCollection) =
        let rec createMap map idx =
            if idx < 0 then map
            else
                let itemName = collection.GetKey idx
                let itemValue = collection.[itemName]
                let map = Map.add itemName itemValue map
                createMap map (idx - 1)
    
        createMap Map.empty (collection.Count - 1)
    

    Imperative loop — Creates the map item-by-item with an imperative loop. As with the recursive loop, I’d prefer to use Array.fold in practice unless there was some special reason not to.

    let mapOfNameValueCollection (collection : NameValueCollection) =
        let mutable map = Map.empty
    
        let maxIndex = collection.Count - 1
        for i = 0 to maxIndex do
            let itemName = collection.GetKey i
            let itemValue = collection.[itemName]
            map <- Map.add itemName itemValue map
    
        map
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a class, which has a NameValueCollection property. public class TestClass
I have to convert a generic object(s) into a NameValueCollection. I am attempting to
In researching how to convert a NameValueCollection to a querystring, I have come across
Can't seem to figure this one out. I have a web service defined as
I need to get the contents of a page (which is just an image)
i have never tried something like this so i need your help. i have
I have an object with a NameValueCollection property which I'm managing with Linq2SQL. I
I have a NameValueCollection with parameter. I want to pass all parameter in SqlCommand
I have some async method public static Task<JObject> GetUser(NameValueCollection parameters) { return CallMethodApi(users.get, parameters,
have written this little class, which generates a UUID every time an object of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.