Export C# expression to json (similar export as to excel, word, html)

Options

Hei,

Title more or less states what I would like to do. I am using queries quite a bit and sometimes it would be useful to get the data as a json string instead of datagrid.

Perhaps this is a trivial / already existing thing, but don't know how to do that.

Just as an example run:
Samples.Where(s => s.SampleId == 1)
displays the datagrid view, which is great. I can click/edit/do whatever.

From this I don't know how to export result as json string.

Comments

  • did you try JsonConvert.SerializeObject(Samples.Where(s => s.SampleId == 1)).Dump();?

  • Thank you. That was not so hard. Works both with Newtonsoft.Json and System.Text.Json.

    JsonConvert.SerializeObject(Samples.Where(s => s.SampleId == 1), Newtonsoft.Json.Formatting.Indented)

    System.Text.Json.JsonSerializer.Serialize<System.Linq.IQueryable<LINQPad.User.Sample>>(Samples.Where(s => s.SampleId == 1), new JsonSerializerOptions { WriteIndented = true })

  • I agree it would be nice to have this as an export option. Of course it's not so hard, but why not make it even easier?