Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity class to Datatable

I'm trying to make a generic class to save log

Here we use entity framework so imagine we have table mng_users(string usr_name, int usr_id) creating the respective class for itentity)

is there a way to implement the toDataTable method converting the entity into a datatable(not a list, having 1 row only) so i can do something like this:

having mng_users1 and mng_users2 as mng_users entity class(both with the same id but diff name)

call method "savelog(mng_users1, mng_users2);" and do the following code:

    private DataTable toDataTable(Object T)
    {
        DataTable vDataTable = new DataTable();

        //AddColums here
        //AddRow with the respective values here

        return vDataTable;
    }

    public void savelog(Object newObject, Object oldObject)
    {

        DataTable newvalue, oldvalue;

        newvalue = toDataTable(newObject);
        oldvalue = toDataTable(oldObject);

       string FieldNames = string.Empty, FieldValuesFrom = string.Empty, FieldValuesTo = string.Empty;
       foreach (DataColumn item in newvalue.Columns)
                {
                    if (newvalue.Rows[0][item].ToString() != oldvalue.Rows[0][item].ToString())
                    {
                        FieldNames += (FieldNames.Length > 0 ? " | " : string.Empty) + item.ColumnName;
                        FieldValuesFrom += (FieldValuesFrom.Length > 0 ? " | " : string.Empty) + newvalue.Rows[0][item].ToString();
                        FieldValuesTo += (FieldValuesTo.Length > 0 ? " | " : string.Empty) + oldvalue.Rows[0][item].ToString();
                    }

                }
        // Save log to sql code here
    }
like image 545
user1859887 Avatar asked Nov 28 '12 13:11

user1859887


2 Answers

My improvement to the above sample:

  • changed syntax to extension method
  • now extension method converts existing object entity list to a DataTable
  • added support for Nullable property types

    public static DataTable ToDataTable<T>(this IEnumerable<T> entityList) where T : class
    {
        var properties = typeof(T).GetProperties();
        var table = new DataTable();
    
        foreach (var property in properties)
        {
            var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
            table.Columns.Add(property.Name, type);
        }
        foreach (var entity in entityList)
        {
            table.Rows.Add(properties.Select(p => p.GetValue(entity, null)).ToArray());
        }
        return table;
    }
    
like image 120
Emil Avatar answered Nov 11 '22 22:11

Emil


Something like the below code should work. It may need to be tweaked depending on whether properties are private/protected and if any of the public properties are indexed, but it should get you started.

private DataTable ToDataTable<T>(T entity) where T : class
{
   var properties = typeof(T).GetProperties();
   var table = new DataTable();

   foreach(var property in properties)
   {
       table.Columns.Add(property.Name, property.PropertyType);
   }

   table.Rows.Add(properties.Select(p => p.GetValue(entity, null)).ToArray());
   return table;
}
like image 12
Jason Meckley Avatar answered Nov 11 '22 21:11

Jason Meckley