代码之家  ›  专栏  ›  技术社区  ›  Tom

在c中动态更改列的数据类型#

c#
  •  0
  • Tom  · 技术社区  · 4 年前

    我正在c#中创建一个数据表,它将由方法a、方法B或方法c这三个方法中的任何一个调用。唯一的区别是,一列的数据类型将根据调用的方法而延迟。此数据表中的attr_value列需要根据调用更改为日期、小数或文本。我希望避免编写三种方法来创建数据表,并且我不确定if-else条件在表定义中是否合适。有人能提出一个解决这个问题的好方法吗。

    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;
            }
    }
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Tim Schmelter    4 年前

    您可以使其通用,并初始化 DataColumn 具有 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;
    }
    

    现在您可以这样称呼它:

    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)
            };
        }
    
    }