A C# datastructure to hold a KeyValue pair with different identifiers than "key" and "value"

Viewed 62

I have a list of books and prices saved in a List<KeyValuePair<string, double>>, I want to send this List in a REST Call, I convert it to a JObject, then send it as the body of my call. It arrives to the REST interface looking something like this:

{"key":BookSample1,"value":"17.99"},
{"key":BookSample2,"value":"21.99"},
{"key":BookSample3,"value":"45.49"}

My problem is that the interface is expecting something like this:

{"Title":BookSample1,"newPrice":"17.99"},
{"Title":BookSample2,"newPrice":"21.99"},
{"Title":BookSample3,"newPrice":"45.49"}

I know that the key and value are readOnly. Is there any way that I can edit the list this way or a different data structure that would let me do that

1 Answers

Convert the original list of KeyValuePairs to a new list of anonymous objects:

theList.Select(kvp => new { Title = kvp.Key, NewPrice = kvp.Value }).ToList();

Serializing this object will give you the proper variable names.

Related