-1

I need to serialise only some fields from Offices, only Name and ID

public IEnumerable<Office> Offices;

public string SerializedPointOffices
{
    get { return JsonConvert.SerializeObject(Offices ); }
}

I have one Linq select

 IEnumerable<MyObj>  offices = GetAllOffices();
 var test = offices.Select(t => new { t.OfficeName, t.OfficeID});

How Serialize this test?

type of test is WhereSelectArrayIterator

Alehandro
  • 19
  • 4

1 Answers1

0

I am not entirely sure of your question, however, assuming you want to serialize an array or list of anonymous types to JSON, its pretty simple.

as above:

var test = offices.Select(t => new { t.OfficeName, t.OfficeID});
string str = JsonConvert.SerializeObject(test);

You can also use the Data Contract serialize annotations on your office object so that only the fields you want get serialized when you serialize the object. Personally, I prefer that way because I don't like sending anonymous types over the wire, I like having a strongly defined interface.

Keith
  • 1,119
  • 2
  • 12
  • 23