2009-12-14 1 views
0

우리는 여전히 DataSets, DataViews 및 DataTables를 사용하는 레거시 모듈이있는 mish-mash 응용 프로그램을 가지고 있지만이 모듈의 DB를 제외하고 대부분 ORMed 된 데이터베이스가 있습니다. 누군가가 우리가 시간이 응용 프로그램과 데이터 집합에서 그 자유의 다른 레이어에 작업을 얻을 것이다 때 알고 내가 그나마개체 DataView 또는 DataSet 또는 DataTable 및 개체로 돌아 가기

/* generates a dataset called CustomerDS with 
DataTable called Customer uses property names as DataColumn name */ 
var dataset =_customer.AsDataSet(); 
/* Converts the dataset to required object or 
throws exception if its cant convert*/ 
var customerEntity = _dataset.ToObject<Customer>(); 

같은 건물 확장에 대해 이동하는 방법에 관해서는 나에게 포인터를 줄 수 있는지 궁금 해서요. 나는 미친 듯이 들릴지도 모르지만 그것의 단지 생각. 그 앱을 지원/버그 수정해야 할 때 나는 악몽을 꾼다.

+1

같이 사용할 수 있습니다 , 아마도 뭔가 : http://stackoverflow.com/questions/564366/generic-list-to-datatable –

+1

그리고 다른 d 개 방향 : http://stackoverflow.com/questions/545328/datatable-to-generic-list-memory-leak –

답변

1

당신은 예를 들어, 반사 사용할 수 있습니다

class Program { 
     public static void Start(string[] args) { 
      var john = new Customer { 
       CustomerID = Guid.NewGuid(), 
       CustomerName = "John", 
       CustomerCode = "J-O" 
      }; 

      var tblJohn = john.ToDataTable(); 
      var clonedJohn = tblJohn.Rows[0].ToDataObject<Customer>(); 
     } 
} 

[AttributeUsage(AttributeTargets.Property)] 
public class DataColumnAttribute : Attribute { } 
public class Customer { 
    [DataColumn] 
    public Guid CustomerID { get; set; } 

    [DataColumn] 
    public string CustomerName { get; set; } 

    [DataColumn] 
    public string CustomerCode { get; set; } 
} 

public static class DataObjectExtensions { 
    public static T ToDataObject<T>(this DataRow dataRow) where T : new() { 
     var dataObject = Activator.CreateInstance<T>(); 
     var tpDataObject = dataObject.GetType(); 

     foreach (var property in tpDataObject.GetProperties()) { 
      var attributes = property.GetCustomAttributes(typeof(DataColumnAttribute), true); 
      if (null != attributes && attributes.Length > 0) { 
       if (property.CanWrite) { 
        DataColumn clm = dataRow.Table.Columns[property.Name]; 
        if (null != clm) { 
         object value = dataRow[clm]; 
         property.SetValue(dataObject, value, null); 
        } 
       } 
      } 
     } 

     return dataObject; 
    } 
    public static DataTable ToDataTable(this object dataObject) { 
     var tpDataObject = dataObject.GetType(); 

     DataTable tbl = new DataTable(); 
     DataRow dataRow = tbl.NewRow(); 
     foreach (var property in tpDataObject.GetProperties()) { 
      var attributes = property.GetCustomAttributes(typeof(DataColumnAttribute), true); 
      if (null != attributes && attributes.Length> 0) { 
       if (property.CanRead) { 
        object value = property.GetValue(dataObject, null); 
        DataColumn clm = tbl.Columns.Add(property.Name, property.PropertyType); 
        dataRow[clm] = value; 
       } 
      } 
     } 

     tbl.Rows.Add(dataRow); 
     tbl.AcceptChanges(); 
     return tbl; 
    } 
} 
0

당신은 데이터 테이블

public static class Extensions 
    { 
     public static List<T> ToList<T>(this DataTable table) where T : new() 
     { 
      IList<PropertyInfo> properties = typeof(T).GetProperties().ToList(); 
      List<T> result = new List<T>(); 

      foreach (var row in table.Rows) 
      { 
       var item = CreateItemFromRow<T>((DataRow)row, properties); 
       result.Add(item); 
      } 

      return result; 
     } 

     private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new() 
     { 
      T item = new T(); 
      foreach (var property in properties) 
      { 
       if (property.PropertyType == typeof(System.DayOfWeek)) 
       { 
        DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), row[property.Name].ToString()); 
        property.SetValue(item,day,null); 
       } 
       else 
       { 
        property.SetValue(item, row[property.Name], null); 
       } 
      } 
      return item; 
     } 
    } 

에서 객체를 얻기 위해 이것을 사용하고 개별 테이블에서이

List<Employee> lst = ds.Tables[0].ToList<Employee>();