Thursday, May 24, 2012

Convert Anonymous Type into key/value Array

The answer in this link is very simple and great:

http://stackoverflow.com/questions/3481923/in-c-sharp-convert-anonymous-type-into-key-value-array


var a = new { data1 = "test1", data2 = "sam", data3 = "bob" };
var type = a.GetType();
var props = type.GetProperties();
var pairs = props.Select(x => x.Name + "=" + x.GetValue(a, null)).ToArray();
var result = string.Join("&", pairs);

No comments: