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


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.


  • Options

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

  • Options

    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 })

  • Options

    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?

Sign In or Register to comment.