Nilesh Barai
Nilesh Barai

Reputation: 1322

Convert IEnumerable<JToken> to IEnumerable<String>

I have this sample Json:

IEnumerable<JToken> a = JArray.Parse("[{'key1':'value1', 'key2':'value2'}]");

I want to convert it to IEnumerable<String> by concatenating all the values. I tried following LINQ query, it performs concatenation correctly but it returns back IEnumerable<JToken>

var t = a.Select(x => x.Values().Aggregate((i, j) => $"{i}|{j}")).ToList();

How do I convert to IEnumerable<String> ?

Upvotes: 0

Views: 1519

Answers (1)

Rufus L
Rufus L

Reputation: 37020

I think if you select the token's Value<string> method (which converts it to a string), it should give you what you're looking for:

List<string> result = a
    .Select(token => token.Values().Aggregate((i, j) => $"{i}|{j}").Value<string>())
    .ToList();

Upvotes: 1

Related Questions