c# - Using Linq to convert KeyValue pairs into Newtonsoft.Json.Linq.JObject -


i trying use c# linq build jobject. know can use loop, such as,

var jobj = new jobject(); foreach (var field in fieldlist) {     jobj[field.name] = new jvalue(field.value); } 

is possible replace loop linq? tried this,

var data = fieldlist.select(field => new keyvaluepair<string, jvalue>(field.name, new jvalue(field.value))); var jobj = new jobject(data); 

but fails error:

could not determine json object type type system.collections.generic.keyvaluepair`2[      system.string,newtonsoft.json.linq.jvalue]. 

here

jobj[field.name] = new jvalue(field.value); 

you calling following jobject indexer:

public jtoken this[string propertyname] { get; set; } 

i.e. setting jobject property.

so linq equivalent this:

var data = fieldlist.select(field => new jproperty(field.name, field.value)); var jobj = new jobject(data); 

Comments