Gitz
Gitz

Reputation: 820

How to convert a list to datatable

I have a class which is converting from JSON text

This is my code

   SearchResult myojb = (SearchResult)js.Deserialize(objText, typeof(SearchResult));

And the SearchResult class is

 public class SearchResult
    {
        public List<Datum> data { get; set; }
        public int page { get; set; }
        public int per_page { get; set; }
        public int total_count { get; set; }
        public string search_id { get; set; }
    }

And DAtum class is

public class Datum
    {
        public string id { get; set; }
        public string description { get; set; }
        public string added_date { get; set; }
        public string media_type { get; set; }
        public Contributor contributor { get; set; }
        public string aspect { get; set; }
        public string image_type { get; set; }
        public bool is_editorial { get; set; }
        public bool is_adult { get; set; }
        public bool is_illustration { get; set; }
        public bool has_model_release { get; set; }
        public bool has_property_release { get; set; }
        public List<string> releases { get; set; }
        public List<Category> categories { get; set; }
        public List<string> keywords { get; set; }
        public Assets assets { get; set; }
        public List<Model> models { get; set; }
    }

I want to convert the List data of SearchResult class into datatable. for conversion I am using https://stackoverflow.com/a/18100872 answer. But not understand how to call it with List data.

Please help stuck with this issue.

Upvotes: 2

Views: 12980

Answers (3)

asif juneja
asif juneja

Reputation: 61

YourList.CreateDataTable("NewNameForYourDataTable");

by the way name for your DataTable is optional so you can write this as well :

YourList.CreateDataTable();

Upvotes: 0

kumar kashyap pandey
kumar kashyap pandey

Reputation: 39

If you already got the searchresult with data from json. As per your Stackoverflow link. The code for converting your list to datatable is as follows:

public static DataTable ToDataTable<T>(List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);

            //Get all the properties
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                //Setting column names as Property names
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    //inserting property values to datatable rows
                    values[i] = Props[i].GetValue(item, null);
                }
                dataTable.Rows.Add(values);
            }
            //put a breakpoint here and check datatable
            return dataTable;
        }

Use will be as follows:

SearchResult myobj = (SearchResult)js.Deserialize(objText, typeof(SearchResult));
            if (myobj.data != null && myobj.data.Count > 0)
            {
                DataTable dt = ToDataTable(myobj.data);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow item in dt.Rows)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            Console.WriteLine(item[dc]);
                        }

                    }
                }
            }

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460288

i only want a Datatable which contains only the rows which are in List<Datum>

Since you already have the method it's simple:

DataTable tblDatum = ToDataTable(myobj.data)

Upvotes: 3

Related Questions