Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think an <code>extension method</code> is the best way to go:</p> <pre><code>public static class Helper { public static T ToType&lt;T&gt;(this DataRow row) where T : new() { T obj = new T(); var props = TypeDescriptor.GetProperties(obj); foreach (PropertyDescriptor prop in props) { if(row.Table.Columns.IndexOf(prop.Name) &gt;= 0 &amp;&amp; row[prop.Name].GetType() == prop.PropertyType) { prop.SetValue(obj, row[prop.Name]); } } return obj; } } </code></pre> <p>Usage:</p> <pre><code>public List&lt;T&gt; ImportTable&lt;T&gt;(String fileName, String table) { //Establish Connection to Access Database File var mdbData = new ConnectToAccess(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\ACCESS\" + fileName + ".mdb;"); var tableData = new List&lt;T&gt;(); foreach (DataRow row in mdbData.GetData("SELECT * FROM " + table).Rows) { tableData.Add(row.ToType&lt;T&gt;()); } return tableData; } </code></pre> <p><b>Update</b> I see that you asked for a <code>Func</code> that would provide the mapping. I'm not sure exactly what you envisioned but here is a method I came up with:</p> <pre><code>public class mdbConcern { public Int32 ConcernId { get; set; } public String Concern { get; set; } public static PropertyDescriptor Mapping(string name) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(mdbConcern)); switch (name) { case "Concern_Id": return props.GetByName("ConcernId"); case "Concern": return props.GetByName("Concern"); default: return null; } } } public static class Helper { public static T ToType&lt;T&gt;(this DataRow row, Func&lt;string, PropertyDescriptor&gt; mapping) where T : new() { T obj = new T(); foreach (DataColumn col in row.Table.Columns) { var prop = mapping(col.ColumnName); if(prop != null) prop.SetValue(obj, row[col]); } return obj; } } </code></pre> <p>Usage:</p> <pre><code>foreach (DataRow row in mdbData.GetData("SELECT * FROM " + table).Rows) { tableData.Add(row.ToType&lt;mdbConcern&gt;(mdbConcern.Mapping)); } </code></pre> <p>Here's a version using attributes on the type's properties to store its mapping. I think it's a more natural solution:</p> <pre><code>[AttributeUsage(AttributeTargets.Property)] public class ColumnMappingAttribute : Attribute { public string Name { get; set; } public ColumnMappingAttribute(string name) { Name = name; } } public class mdbConcern { ColumnMapping("Concern_Id")] public Int32 ConcernId { get; set; } ColumnMapping("Concern")] public String Concern { get; set; } } public static class Helper { public static T ToType&lt;T&gt;(this DataRow row) where T : new() { T obj = new T(); var props = TypeDescriptor.GetProperties(obj); foreach (PropertyDescriptor prop in props) { var columnMapping = prop.Attributes.OfType&lt;ColumnMappingAttribute&gt;().FirstOrDefault(); if(columnMapping != null) { if(row.Table.Columns.IndexOf(columnMapping.Name) &gt;= 0 &amp;&amp; row[columnMapping.Name].GetType() == prop.PropertyType) { prop.SetValue(obj, row[columnMapping.Name]); } } } return obj; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload