Tom
Tom

Reputation: 8681

Dynamically changing the datatype of the column in c#

I am creating a datable in c# which will be called by any of the three methods method A, method B or Method C. The only difference is that one column will defer in datatype based on which method is calling. The attr_value column in this datable would need to change to date, decimal or text based on the call. I want to avoid writing three methods to create a datatable and I am not sure if an if else condition would be appropriate within the table definition. Could somebody advice a good approach to tackle this.

public class AttributeValueInsertDate : EntityBase
    {
        public static object ResolveRequest(AttributeValueList attributeValueList)
        {
            return new
            {
                dateValuesTVP = AttributeValue.CreateAttributeValueDataTable(attributeValueList)
            };
        }
    }

 public class AttributeValueInsertDecimal : EntityBase
    {
        public static object ResolveRequest(AttributeValueList attributeValueList)
        {
            return new
            {
                decimalValuesTVP = AttributeValue.CreateAttributeValueDataTable(attributeValueList)
            };
        }

    }

 public class AttributeValueInsertText : EntityBase
    {
        public static object ResolveRequest(AttributeValueList attributeValueList)
        {
            return new
            {
                textValuesTVP = AttributeValue.CreateAttributeValueDataTable(attributeValueList)
            };
        }

    }

public class AttributeValue
{
public static DataTable CreateAttributeValueDataTable(AttributeValueList attributeValueList)
        {
            var dataTable = new DataTable();
            dataTable.Columns.Add("attr_id", typeof(Int32));
            dataTable.Columns.Add("attr_value", typeof(DateTime));
            dataTable.Columns.Add("effective_date", typeof(DateTime));
            dataTable.Columns.Add("expiration_date", typeof(DateTime));
            dataTable.Columns.Add("last_updated_user", typeof(string));
            dataTable.Columns.Add("last_updated_date_time", typeof(DateTime));

            if (attributeValueList.Count > 0)
            {
                foreach (var item in attributeValueList)
                {
                    DataRow dr = dataTable.NewRow();
                    dr[0] = item.AttributeId;
                    dr[1] = item.Value;
                    dr[2] = item.EffectiveDate;
                    dr[3] = item.ExpirationDate;
                    dr[4] = item.LastUpdatedUser;
                    dr[5] = item.LastUpdatedDate;

                    dataTable.Rows.Add(dr);
                }
            }
            return dataTable;
        }
}

Upvotes: 0

Views: 180

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460058

You can make it generic and initialize the DataColumn with typeof(T):

public static DataTable CreateAttributeValueDataTable<T>(AttributeValueList attributeValueList)
{
    var dataTable = new DataTable();
    dataTable.Columns.Add("attr_id", typeof(Int32));
    dataTable.Columns.Add("attr_value", typeof(T));
    dataTable.Columns.Add("effective_date", typeof(DateTime));
    dataTable.Columns.Add("expiration_date", typeof(DateTime));
    dataTable.Columns.Add("last_updated_user", typeof(string));
    dataTable.Columns.Add("last_updated_date_time", typeof(DateTime));

    if (attributeValueList.Count > 0)
    {
        foreach (var item in attributeValueList)
        {
            DataRow dr = dataTable.NewRow();
            dr[0] = item.AttributeId;
            dr[1] = item.Value;
            dr[2] = item.EffectiveDate;
            dr[3] = item.ExpirationDate;
            dr[4] = item.LastUpdatedUser;
            dr[5] = item.LastUpdatedDate;

            dataTable.Rows.Add(dr);
        }
    }
    return dataTable;
}

Now you can call it in this way:

public class AttributeValueInsertDate : EntityBase
{
    public static object ResolveRequest(AttributeValueList attributeValueList)
    {
        return new
        {
            dateValuesTVP = AttributeValue.CreateAttributeValueDataTable<DateTime>(attributeValueList)
        };
    }
}

public class AttributeValueInsertDecimal : EntityBase
{
    public static object ResolveRequest(AttributeValueList attributeValueList)
    {
        return new
        {
            decimalValuesTVP = AttributeValue.CreateAttributeValueDataTable<Decimal>(attributeValueList)
        };
    }

}

public class AttributeValueInsertText 
{
    public static object ResolveRequest(AttributeValueList attributeValueList)
    {
        return new
        {
            textValuesTVP = AttributeValue.CreateAttributeValueDataTable<string>(attributeValueList)
        };
    }

}

Upvotes: 1

Related Questions