How to return dynamic object from SQL query

后端 未结 3 2039
猫巷女王i
猫巷女王i 2020-12-21 14:16

I have situation where a storeprocdure return collection, but I do not how the object structure because the query is very dynamic.

One query can return:

3条回答
  •  有刺的猬
    2020-12-21 15:05

    If on SQL 2016 or newer, add "FOR JSON AUTO" to your query to return as JSON, e.g:

    var json = db.Database.SqlQuery("Select x, y, z FROM tbl FOR JSON AUTO").First();
    

    Then use Json.Net to create a dynamic object using

    var myDynamic = JObject.Parse(json)
    

提交回复
热议问题