How can I return a list of rows of dynamic table? [duplicate]

℡╲_俬逩灬. 提交于 2020-01-07 08:36:12

问题


My previous question was closed due to typo. But my issue is unresolved. So rephrasing the question.

Hi,

I have a query which returns data dynamically. Number of columns, their name and datatypes are not known. It's like a

Select * from table_name

I need to get the result in a list. I tried with IEnumerable.

[Authorize]
    [Route("GetRecord")]
    public IEnumerable<Object> GetRecord()
    {
        using (var ctx = new IMDBEntities())
        {
            var recordList = ctx.Database.SqlQuery<Object>("Select * from tbl").ToList();
            return recordList;
        }
    }

When I debug this code, I get this result

[{},{}]

Currently in a test table there are 2 records with 5 columns. It shows there are 2 records but empty.

I worked on @NetMage suggestion. And my updated code is as below.

        [Authorize]
[Route("GetRecord")]
public IEnumerable<DataRow> GetRecord()
{
    var table = new DataTable();
    using (var da = new SqlDataAdapter("SELECT * FROM mv_tbl", "ConnectionString"))
    {
        da.Fill(table);
        List<DataRow> list = new List<DataRow>();
        foreach (DataRow r in table.Rows)
        {
            list.Add(r);
        }

        return list;
    }
}

Now it gives me result but also gives much more unnecessary data. Below is the result.

[{"RowError":"",
"RowState":2,
"Table":[{"mv_id":2,
"mv_name":"Up",
"mv_link":"link",
"user":"admin@gmail.com",
"db_tstamp":"2020-01-01T01:50:00"},
{"mv_id":3,
"mv_name":"8 Below",
"mv_link":"link",
"user":"admin@gmail.com",
"db_tstamp":"2020-01-01T02:49:00"}],
"ItemArray":[2,"Up","link","admin@gmail.com","2020-01-01T01:50:00"],
"HasErrors":false},
{"RowError":"",
"RowState":2,
"Table":[{"mv_id":2,
"mv_name":"Up",
"mv_link":"link",
"user":"admin@gmail.com",
"db_tstamp":"2020-01-01T01:50:00"},
{"mv_id":3,
"mv_name":"8 Below",
"mv_link":"link",
"user":"admin@gmail.com",
"db_tstamp":"2020-01-01T02:49:00"}],
"ItemArray":[3,"8 Below","link","admin@gmail.com","2020-01-01T02:49:00"],
"HasErrors":false}]

Expected result is:

[{"mv_id":2,
    "mv_name":"Up",
    "mv_link":"link",
    "user":"admin@gmail.com",
    "db_tstamp":"2020-01-01T01:50:00"},
    {"mv_id":3,
    "mv_name":"8 Below",
    "mv_link":"link",
    "user":"admin@gmail.com",
    "db_tstamp":"2020-01-01T02:49:00"}]

Hi, It just needed return DataTable. This has been answered here.

DataTable iteration gives unwanted data

After all suggestions, this is my working code.

[Authorize]
        [Route("GetRecord")]
        public DataTable Records(MyViewModel model)
        {
            var table = new DataTable();
            using (var da = new SqlDataAdapter(model.qstring, "ConnString"))
            {
                //DataSet ds = new DataSet();
                da.Fill(table);
                return table;
            }
        }

来源:https://stackoverflow.com/questions/59549332/how-can-i-return-a-list-of-rows-of-dynamic-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!