-1

I created a datatable in my application

DataTable table = new DataTable();

It has the following format

    ID    |  Name     |  Add    |   Edit  | View   | Authorize
    ---------------------------------------------------------------------
    10    | User      | true    |  trues  | trues  |  true
    11    | Group     | true    |  trues  | trues  |  true
    12    | Permission| true    |  trues  | trues  |  true

I want to retrieve one row from this datatable by id.

For example I want to retrieve the row with id = 12

That is,

  12    | Permission| true    |  trues  | trues  |  true

After that I want to convert only this row to json string using json.net

{["id":"12","name":"Permission","add":true,"edit":true,"authorize":true,"view":true}]

Is it possible? Please help

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
folk
  • 687
  • 3
  • 8
  • 16

2 Answers2

2

You may use LINQ and Json.NET. At the top your class add:

using System.Linq;

In your code, you may have 2 approaches:

// 1. select the row in the table
// 2. create an anonymous object
// 3. serialize it
var json1 = JsonConvert.SerializeObject(table.Select("ID=12")
    .Select(row => new
    {
        id = row["ID"],
        name = row["Name"],
        add = row["Add"],
        edit = row["Edit"],
        view = row["View"],
        authorize = row["Authorize"]
    }).FirstOrDefault());

// 1. cast all rows in the table
// 2. create a collection of anonymous objects
// 3. select the anonymous object by id
// 4. serialize it
var json2 = JsonConvert.SerializeObject(table .Rows
    .Cast<DataRow>()
    .Select(row => new
    {
        id = row["ID"],
        name = row["Name"],
        add = row["Add"],
        edit = row["Edit"],
        view = row["View"],
        authorize = row["Authorize"]
    })
    .FirstOrDefault(row => row.id.ToString() == "12"));

Alternatively, you may use your own class. This option doesn't necessarily requires LINQ:

// 1. select the row
// 2. create a new TableRow object
// 3. serialize it
var filteredRow = table.Select("ID=12");
if (filteredRow.Length == 1)
{
    var json3 = JsonConvert.SerializeObject(
        new TableRow(filteredRow[0].ItemArray));
}

A simplified TableRow class definition might look as the following:

public class TableRow
{
    public int id { get; set; }
    public string name { get; set; }
    public bool add { get; set; }
    public bool edit { get; set; }
    public bool view { get; set; }
    public bool authorize { get; set; }

    public TableRow(object[] itemArray)
    {
        id = Int32.Parse(itemArray[0].ToString());
        name = itemArray[1].ToString();
        add = bool.Parse(itemArray[2].ToString());
        edit = bool.Parse(itemArray[3].ToString());
        view = bool.Parse(itemArray[4].ToString());
        authorize = bool.Parse(itemArray[5].ToString());
    }
}
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
0

How about using Select method of DataTable

DataRow[] dr = table.Select("id = 12");

Or to be dynamic

int rowid = ... //assign some value through code and then
DataRow[] dr = table.Select("id = " + rowid);

I have no idea about JSON but you can look at

Convert DataTable to JSON with key per row
http://www.codeproject.com/Tips/624888/Converting-DataTable-to-JSON-Format-in-Csharp-and
C# DataTable to Json?

Community
  • 1
  • 1
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208