0

我有一个像这样返回一个选择的存储过程:

select name , family , sex from person

我有一门这样命名的人:

 public class person {
         string name {get;set;}
         string family {get;set;}
         GenderEnum sex {get;set;}
       }

我有一个 Enum 命名为 GenderEnum ,如下所示:

Enum GenderEnum
{
Man = 1 ,
Woman = 2 ,
}

我想将我的数据表转换为 IList of Person 。

我的问题是:当我想将性别列转换为性别字段时,我最常将 int 类型转换为 GenderEnum。

不要忘记,我最有一个通用方法将所有 DataTable 转换为 All Class,并且这个方法最能识别每个属性都有枚举类型。

此方法根据名称将所有列映射到所有属性

我写了这个方法,但这不适用于所有类型。

例如:如果我的列是 int 并且我的属性是 strinbe 并且具有相同的名称,则映射不起作用。

public List<T> ConvertTo<T>(DataTable datatable) where T : new()
    {
        var temp = new List<T>();
        try
        {
            var columnsNames = (from DataColumn dataColumn in datatable.Columns select dataColumn.ColumnName).ToList();
            temp = datatable.AsEnumerable().ToList().ConvertAll<T>(row => GetObject<T>(row, columnsNames));
            return temp;
        }
        catch
        {
            return temp;
        }

    }
private T GetObject<T>(DataRow row, List<string> columnsName) where T : new()
    {
        T obj = new T();
        try
        {
            string columnname = "";

            PropertyInfo[] Properties = typeof(T).GetProperties();
            foreach (PropertyInfo objProperty in Properties)
            {
                columnname = columnsName.Find(name => name.ToLower() == objProperty.Name.ToLower());
                if (!string.IsNullOrEmpty(columnname))
                {
                    var value = row[columnname];

                    if (!string.IsNullOrEmpty(value.ToString()))
                    {

                        Type type;

                        type = Nullable.GetUnderlyingType(objProperty.PropertyType) ?? objProperty.PropertyType;

                        objProperty.SetValue(obj,
                                             type == value.GetType()
                                                 ? Convert.ChangeType(value, type)
                                                 : System.Enum.ToObject(type, value), null);
                    }
                }
            }
            return obj;
        }
        catch (Exception exception)
        {
            return obj;
        }
    }
4

2 回答 2

4

I think you're asking for trouble with this GetObject method and you'd be better off to switch to EF or (virtually) any other ORM since what you're doing is extremely risky. Consider the maintainability and readability of this code. However, I understand that you probably are under pressure of a deadline and can't do much about it at this stage.

To provide a quick fix for your problem, please consider modifying your Person class as follows:

public class Person
{
    public String Name { get; set; }

    public String Family { get; set; }

    public Int32 Sex  { get; set; }

    public GenderEnum EnumeratedSex
    {
        get
        {
            return Sex == 1 ? GenderEnum.Man : GenderEnum.Woman;
        }
    }
}

public enum GenderEnum
{
    Man = 1,
    Woman = 2
}

I believe that your Sex field is simply an int in the database, but if not, please change it accordingly.

Please let me know if this is of any help.

于 2013-03-27T14:46:25.353 回答
1

而不是这样做,为什么不尝试使用实体框架呢?请参阅此链接以供参考

于 2013-03-27T10:24:35.430 回答