Monday, 6 January 2014

7 way to convert DataTable to generic List - C#

Leave a Comment
Conversion DataTable to Generic List<t>. We can do it in various way.

Solution 1:

DataTable dt = CreateDataTable();
List<datarow> list = new List<datarow>();
foreach (DataRow dr in dt.Rows)
{
     list.Add(dr);
}

Solution 2:

DataTable table = new DataTable {
Columns = {
{"Foo", typeof(int)},
{"Bar", typeof(string)}
}
};

for (int i = 0; i < 5000; i++) {
     table.Rows.Add(i, "Row " + i);
}

List<T> data = new List<t>(table.Rows.Count);
foreach (DataRow row in table.Rows) {
      data.Add(new T((int)row[0], (string)row[1]));
}

Solution 3:

Using Linq expression. It return data in List<t>.

List<string> list =dataTable.Rows.OfType<datarow>().Select(dr => dr.Field<string>(0)).ToList();

Solution 4:

Using Linq/lamda expression.

List<employee> list= new List<employee>();
list = (from DataRow row in dt.Rows

select new Employee
{
     FirstName = row["ColumnName"].ToString(),
     LastName = row["ColumnName"].ToString()
}).ToList();


Solution 5:

Using Linq expression.

List<t> target = dt.AsEnumerable()
.Select(row => new T
{
     // assuming column 0's type is Nullable<long>
     ID = row.Field<long?>(0).GetValueOrDefault()
     Name = String.IsNullOrEmpty(row.Field<string>(1))
     ? "not found"
     : row.Field<string>(1)
})
.ToList();


Solution 6:

Using Linq/lamda expression. All are return array of datarow in List<t>.

List<datarow> list1= dataTable.Select().ToList();
List<datarow> list2= dataTable.Rows.Cast<datarow>().ToList();
List<datarow> list3 = dataTable.AsEnumerable().ToList();
List<datarow> list4 = new List<datarow>(dataTable.select());

Here result will return all rows of datatable, as an array of datarows, and the List constructor accepts that array of objects as an argument to initially fill our List<datarow> with.

Solution 7:

Using reflection PropertyInfo class.(.Net4)

sealed class Tuple<T1, T2>
{

       public Tuple() {}
       public Tuple(T1 value1, T2 value2) {Value1 = value1; Value2 = value2;}
       public T1 Value1 {get;set;}
       public T2 Value2 {get;set;}
}



public static List<T> Convert<T>(DataTable table)

where T : class, new()
{
      List<Tuple<DataColumn, PropertyInfo>> map =
      new List<Tuple<DataColumn,PropertyInfo>>();
      foreach(PropertyInfo pi in typeof(T).GetProperties())
      {

         ColumnAttribute col = (ColumnAttribute)
         Attribute.GetCustomAttribute(pi, typeof(ColumnAttribute));
         if(col == null) continue;
         if(table.Columns.Contains(col.FieldName))
         {
             map.Add(new Tuple<DataColumn,PropertyInfo>(
             table.Columns[col.FieldName], pi));
         }
      }

      List<T> list = new List<T>(table.Rows.Count);
      foreach(DataRow row in table.Rows)
      {
          if(row == null)
          {
              list.Add(null);
              continue;
          }

          T item = new T();
          foreach(Tuple<DataColumn,PropertyInfo> pair in map) {
              object value = row[pair.Value1];
              if(value is DBNull) value = null;
              pair.Value2.SetValue(item, value, null);
          }

         list.Add(item);
   }

   return list; 

}

 


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.