intersect a dictionary and a list to return another dictionary

Alex Gordon

How do you pass in a list of headers and get back the key/value pairs?

I've created a method like the following:

public static IDictionary<string, string> GetHeaderValues(
    IReadOnlyList<string> keys, IHeaderDictionary headers)
{
}

I'd like to pass in a list of string such as "trackingId, requestId, corrId" and get back a dictionary like so:

trackingId: 123123
requestId: abc123123
corrId: xyz123

The purpose of this is to pass in ALL the headers, and retrieve only the desired headers.

How do we intersect these two objects IReadOnlyList and IHeaderDictionary and map into a regular IDictionary?

I've attempted to intersect the two with the following:

headers.Keys.Intersect(keys); however this will return an ienumerable string.

itminus

a short pseudo-code :

intersect(keys1,keys2)        // get common keys list
    |> map to (key,value)     // map to key-value pair list
    |> to dictionary           // convert list to dict

C# Implementation :

public static IDictionary<string, string> GetHeaderValues(IReadOnlyList<string> keys, IHeaderDictionary headers)
{
    return keys.Intersect(headers.Keys)
        .Select(k => new KeyValuePair<string,string>(k,headers[k]))
        .ToDictionary(p => p.Key, p => p.Value);
}

Test Case :

var list = new List<string>{
    "Content-tyb3",  // non-exist
    "Cookie",        
    "Accept-Language",
    "Program",      // non-exist
};
var headers = GetHeaderValues(list,Request.Headers);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C#: Intersect a dictionary with a list. Return dictionary item after match

Sort dictionary by another list or dictionary

Python 2.7 - Intersect Unicode Dictionary with Unicode List

Find keys in dictionary with values that intersect with list

Read a dictionary list, within another dictionary list

Map elements of a list of dictionary from another dictionary

Appending a dictionary containing a list to another dictionary in python

how to return a list of dictionary in graphene?

Dictionary to return a character list with their indices

Divide list of values in dictionary by list of values in another dictionary

Map a dictionary with another dictionary

Merging dictionary with another dictionary

Copying a dictionary into another dictionary

Dictionary from another dictionary

Add list as keys for another list and convert to dictionary

Cannot return a VBA dictionary to another function

Python - Return the dictionary key comparing dictionary values with list

Comparing keys of one dictionary to another dictionary with a list of values

How to find list contents in dictionary and store it in another dictionary using Python

Replacing items in a list in a dictionary with items from another dictionary

Grouping dictionary data from a list and storing in another dictionary

Need help to understand and retrieve ansible dictionary from list in another dictionary

covert dictionary to a list of dictionary

Find an element from a list in a dictionary and return that key

Python Dictionary: Return First Value in List Or String

Printing a zip from dictionary return Empty list

Return dictionary keys based on list integer value

Return keys of dictionary if values match with a given list

How to append the return of a function to a list and a dictionary in a for loop