Click here to Skip to main content
15,849,996 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to dynamically show a table depending on what tableName user has selected from dropdown. I am passing a json object from my web Controller(.Net Core) so in order to do it, I am first converting my dataTable to list of objects using function
C#
public static List<T> ConvertTableToList<T>(this DataTable table) where T : class, new()
        {
            try
            {
                List<T> list = new List<T>();
            foreach (var row in table.AsEnumerable())
            {
                T obj = new T();

                foreach (var prop in obj.GetType().GetProperties())
                {
                    try
                    {
                        PropertyInfo propertyInfo = obj.GetType().GetProperty(prop.Name);
                        propertyInfo.SetValue(obj, Convert.ChangeType(row[prop.Name], propertyInfo.PropertyType), null);
                    }
                    catch(Exception ex)
                    {
                        throw ex;
                    }
                }

                list.Add(obj);
            }

            return list;
        }
        catch
        {
            return null;
        }
    }

and call this function in my Get Request
C#
public IActionResult GetTableDetailsByTableName(string TableNameSelected)
{
    //To get the data for the Table Selected
    DataTable TableDetails = ReferenceTableLogics.getTableDetails(TableNameSelected);

    var TableDetailsInList = ConverterClass.ConvertTableToList<CSBM_AGE_BAND>(TableDetails);
    return Ok(TableDetailsInList);
}

Now the issue is that I need to tell my class Name (eg CSBM_AGE_BAND in this case) depending on what user has selected in dropdown (TableNameSelected).

Is there any way by which I can dynamically pass this class name to my function ConvertTableToList() ?

What I have tried:

I tried doing it by reflection but didn't help much.

I would also like to know that if any function exists in .net core which can directly convert dataset to json object (tried JsonConvert.SerializeObject but wasn't able to access it by typescript). This is how I am retrieving at client side.

C#
getTableColumnDetailsByTableName(TableName: string): void {
        this.coverMeService.getTableColumnDetailsByTableName(TableName)
            .subscribe(data => { this.TableColumnDetails = data; } );
    }
Posted
Updated 24-Apr-18 6:32am
v3

1 solution

Use a non-generic method to convert the DataTable:
C#
public static List<object> ConvertTableToList(this DataTable table, Type itemType)
{
    List<object> result = new List<object>();
    
    foreach (var row in table.AsEnumerable())
    {
        object item = Activator.CreateInstance(itemType);
        
        foreach (var prop in itemType.GetProperties())
        {
            prop.SetValue(item, Convert.ChangeType(row[prop.Name], prop.PropertyType), null);
        }
        
        result.Add(item);
    }
    
    return result;
}
Usage:
C#
Type itemType = GetItemTypeFromTableName(TableNameSelected);
List<object> TableDetailsInList = ConverterClass.ConvertTableToList(TableDetails, itemType);

Now you just need to work out how to map from the table name to the item type. For example:
C#
private static readonly IReadOnlyDictionary<string, Type> ItemTypeMap = new Dictionary<string, Type>
{
    ["CSBM_AGE_BAND"] = typeof(CSBM_AGE_BAND),
    ...
};

private static Type GetItemTypeFromTableName(string tableName) => ItemTypeMap[tableName];
 
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