如何将LinqPAD中的数据导出为JSON? [英] How to export data from LinqPAD as JSON?
问题描述
我想创建一个JSON文件,用作简单的Web原型制作练习的一部分. LinqPAD非常适合按我需要的形状从数据库访问数据,但是我无法很轻松地将其作为JSON导出.
I want to create a JSON file for use as part of a simple web prototyping exercise. LinqPAD is perfect for accessing the data from my DB in just the shape I need, however I cannot get it out as JSON very easily.
我实际上并不在乎模式是什么,因为我可以调整我的JavaScript以使其能够处理返回的任何内容.
I don't really care what the schema is, because I can adapt my JavaScript to work with whatever is returned.
这可能吗?
推荐答案
更流畅的解决方案是将以下方法添加到Linqpad中的我的扩展"文件中:
A more fluent solution is to add the following methods to the "My Extensions" File in Linqpad:
public static String DumpJson<T>(this T obj)
{
return
obj
.ToJson()
.Dump();
}
public static String ToJson<T>(this T obj)
{
return
new System.Web.Script.Serialization.JavaScriptSerializer()
.Serialize(obj);
}
然后,您可以在需要的任何查询中像这样使用它们:
Then you can use them like this in any query you like:
Enumerable.Range(1, 10)
.Select(i =>
new
{
Index = i,
IndexTimesTen = i * 10,
})
.DumpJson();
我分别添加了"ToJson",以便可以与"Expessions"一起使用.
I added "ToJson" separately so it can be used in with "Expessions".
这篇关于如何将LinqPAD中的数据导出为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!