Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
problem

data display on strange format How to convert it to json format 


I make function return list of object data of column name where pass table name

meaning function return column names joined with table reference

but problem data display in strange format as below :

the result as following

[1,1,"كود الموظف","EmployeeId","Employees",2,2,"الفرع","BranchCode","Employees",3,3,"الاسم","EmployeeName","Employees"]

How to display data above on json format meaning show data as
as [rowno:1,code:1,arabiccaption:كود الموظف ,etc.....


What I have tried:

[HttpGet("{tableName}")]
       [Produces("application/json")]
       public List<object> GetColumnNames([FromRoute] string tablename)
       {

           var columnNames = new List<object>();
           using (var command = _context.Database.GetDbConnection().CreateCommand())
           {

               command.CommandText = $"SELECT row_number() over (order by code asc ) as rownum,code ,ArabicCaption, COLUMN_Name,TableName  FROM INFORMATION_SCHEMA.COLUMNS left join ReferenceFiles on INFORMATION_SCHEMA.COLUMNS.COLUMN_Name=ReferenceFiles.FieldName and INFORMATION_SCHEMA.COLUMNS.TABLE_NAME=ReferenceFiles.TableName WHERE TableName =  N'{tablename}'  and TABLE_NAME= N'{tablename}'";

               _context.Database.OpenConnection();
               using (var reader = command.ExecuteReader())
               {
                   if (reader.HasRows)
                   {
                       while (reader.Read())
                       {
                           columnNames.Add((Int64)reader["rownum"]);
                           columnNames.Add((Int32)reader["code"]);
                           columnNames.Add((string)reader["ArabicCaption"]);
                           columnNames.Add((string)reader["COLUMN_Name"]);
                           columnNames.Add((string)reader["TableName"]);
                       }
                   }
                   else
                   {
                       columnNames.Add("Not Found");
                   }
               }
           }
           return columnNames;

       }
Posted
Updated 21-Mar-19 8:18am
v2

1 solution

You're returning an array containing the value of every column from every row, flattened out into a single list. You need to return an array of objects instead:
C#
while (reader.Read())
{
    columnNames.Add(new
    {
        rownum = (Int64)reader["rownum"],
        code = (Int32)reader["code"],
        arabicCaption = (string)reader["ArabicCaption"],
        columnName = (string)reader["COLUMN_Name"],
        tableName = (string)reader["TableName"]
    });
}

The returned data will look something like:
[
    {
        rownum: 1,
        code: 1,
        arabicCaption: "كود الموظف",
        columnName: "EmployeeId",
        tableName: "Employees"
    },
    {
        rownum: 2,
        code: 2,
        arabicCaption: "الفرع",
        columnName: "BranchCode",
        tableName: "Employees"
    },
    {
        rownum: 3,
        code: 3,
        arabicCaption: "الاسم",
        columnName: "EmployeeName",
        tableName: "Employees"
    }
]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900