How to convert typed DataTable into List of Entity?

I would check out AutoMapper which is a great tool that focus on these kind of scenarios.

Also here is guy that have done exactly your scenario.

Hope this was helpful!


One way could be by doing it through custom code Lets suppose you Entity class name is 'MyEnt'

 public class MyEnt
    {
     public string Name {get; set;}
     public string Type {get; set;}
     public LoadMyEnt(object [] array)
      {
         this.Name = array[0].ToString();
         this.Type = array[1].ToString();   
      }
    }

//for datatable, you could do

List<MyEnt> list = new List<MyEnt>();
foreach(DataRow dr in table.Rows)
{
  MyEnt obj = new MyEnt();
obj.Load(dr.ItemArray);
list.Add(obj);
}

You can first convert it to generic datatable and then convert that datatable to list. There are plenty of code snippets available to convert generic datatable to list. Some are: How do you convert a DataTable into a generic list? Convert DataTable to List<T> Fastest way to convert datatable to generic list


You could write an Extension Method:

public static List<TItem> Cast<TItem>(this DataRowCollection drc, Func<DataRow, TItem> converter)
    {
        List<TItem> list= new List<TItem>();
        foreach (DataRow row in drc)
        {
            list.Add(converter(row));
        }
        return list;
    }

Wherein converter is e.g. a lambda expression containing the following:

item => new YourClass(item)

Thanks,

Dennis


Comments

  1. Leroy

    • 2018/11/18

    I need to convert the data of a Typed DataTable to List of my entity. I've Mapped all the fields of entity with field of DataTable. Any ideas?

  2. Chandler

    • 2020/4/27

    I need to convert the data of a Typed DataTable to List of my entity. I've Mapped all the fields of entity with field of DataTable. Any ideas?

  3. Guillermo

    • 2017/9/1

    This is a generic method that will convert any type of DataTable to a List (the DataTable structure and List class structure should be the same) 

  4. Anthony

    • 2018/1/22

    Recently some one asked me how should I need to convert a DataTable to an List of Entity Object. Let me Explain in detail below… Let say we have following Entity Class called Testimonial with two properties i.e.., Comment and Commentedby here Comment is the property where we have the actual comment by the User and Commentedby is the property to store the name of the User who commented it.

  5. Kingston

    • 2019/10/17

    public static List<T> DataTableToEntityList<T>(DataTable dt). {. Logging.Info("Converting datatable to entity list");. foreach (DataColumn column in dt.

  6. Harvey

    • 2021/7/8

    Convert DataTable to List using a Generic Method This is a generic method that will convert any type of DataTable to a List (the DataTable structure and List class structure should be the same). The following are the two functions in which if we pass a DataTable and a user defined class.

  7. Ibrahim

    • 2019/7/26

    This snippet helps you convert a datatable to a list of entities. using res = new List (); if (table == null) return res; Type classType 

  8. Jakob

    • 2020/8/23

    NB:Just i want to convert a datatable to a list.You may not consider my above code.Just give me the code to convert a DataTable to a List. I am using .Net Framework 4.0 Thanks in advance

  9. Joey

    • 2020/11/30

    Recently some one asked me how should I need to convert a DataTable to an List of Entity Object. Let me Explain in detail below…

  10. Justin

    • 2016/12/31

    This tips gives code for converting the EntityCollection of a DataTable. Using the code. The Dynamics CRM 2011 RetriveMultiple method returns an EntityCollection. The below code is to convert th EntityCollection to a DataTable.

  11. Eliseo

    • 2016/1/19

    Represents one table of in-memory data. public class DataTable : System. Type.GetType("System.Int32"); column.ColumnName = "id"; column.

  12. Williams

    • 2020/10/17

    In this blog, we will discuss the simple common solution to convert an object of type DataTable into a list of Models in C#. I have tried my best to explain the use of the common generic method to convert any datable to Model List.

  13. Tripp

    • 2018/12/14

    Then we can convert Datatable of above class into List using public static class Helper { private static readonly IDictionary<Type, 

  14. Malachi

    • 2015/1/11

    the above method is widely used nowadays to convert datatable into List, but second method of foreach is better performance wise than LINQ, to test this I tried to execute both the (2 & 3)methods by creating a new console project in Visual Studio, here is the code for complete console project (method 1 is not tested by me for performance as it

  15. Conner

    • 2015/10/31

    1 #region Datatale to entity list 2 /// <summary> 3 /// Datatale to entity list 4 /// </summary> 5 /// <typeparam name="T">Entity class type</typeparam> 6 

  16. Ricci

    • 2016/10/24

    This article describes how to convert Data Table to List of Objects. In following code, you can simply convert DataTable to object type List in C#.

  17. Brian

    • 2018/11/14

    [C#] Generic Convert To Entity, List, DataTable 1. (this DataRow tableRow) where T : new() { // Create a new type of the entity I..

Comments are closed.

Recent Posts