Click here to Skip to main content
13,800,314 members
Rate this:
 
Please Sign up or sign in to vote.
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
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
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.

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

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Use a non-generic method to convert the DataTable:
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:
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:
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];
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.181214.1 | Last Updated 24 Apr 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100