jeudi 21 janvier 2021

Regarding Dynamic List to DataTable and Use of TypeDescriptor

I know that this question has been asked here before and a number of solutions were also suggested, however, after going through a lot of posts and trying many many suggested solutions, I was able to get the desired result using the simple code given below in **Style 3** while others failed. I wanted to ask why Style 2 failed and how Style 3 worked. I was under the impression that System.Component.TypeDescriptor is what we are supposed to use to get property names from Dynamic objects. Any information that would help me to understand will be greatly appreciated. Thank You!

public void EmailExcel([FromBody] MvJson json)
    {
        var dict = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json.Json);
        dynamic dyn = dict["gridData"];
        dynamic off = dict["OfficeIdList"];
        List<dynamic> responseList = new List<dynamic>(dyn);
        List<dynamic> officeIdList = new List<dynamic>(off);
        if (officeIdList != null)
        {
            foreach (var item in officeIdList)
        {
        var gridData = responseList.FindAll(x => x.OfficeId == item.OfficeId);
        DataSet ds = new DataSet();
        DataTable dt = ToDataTable(gridData);  <<<<---ISSUE
        ds.Tables.Add(dt);
        var fd = ConvertToExcelByte(ds);    
    }

**<<<<< STYLE 1 >>>>>>**
        public DataTable ToDataTable<T>(List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);
            <<<<<ISSUE>>>>>
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {

                    values[i] = Props[i].GetValue(item, null);
                }
                dataTable.Rows.Add(values);
            }
            return dataTable;
        }

**<<<<< STYLE 2 >>>>>>**
        public static DataTable ToDataTable<T>(List<T> items)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
            {
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            }
            foreach (T item in items)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                {
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                }

                table.Rows.Add(row);
            }
            return table;
        }

**<<<<< STYLE  >>>>>>**   **WORKED**
        public DataTable ToDataTable<T>(List<T> items)
        {
            var json = JsonConvert.SerializeObject(items);
            DataTable dataTable = new DataTable();
            dataTable = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
            return dataTable;
        }





Aucun commentaire:

Enregistrer un commentaire