How can I convert a DataTable
in IEnumerable<dynamicObject>
?
For example, I want to convert any DataTable
ID | Name DI | emaN --------- or --------- 1 | x 2 | x 2 | y 1 | y
In a list of objects
// list 1 (ex 1) // list 2 (ex 2) { { { ID = 1, Name = "x" } { DI = 2, emaN = "x" } { ID = 2, Name = "y" } { DI = 1, emaN = "y" } } }
So
list1.First().ID // 1 list2.First().emaN // "x"
How can I do it?
Using the Code NET reflection in order to be able to do it dynamically. The idea is pretty simple. Fill the object with the related field in the DataRow based on the field name, but to be able to do it smoothly, you need to have the DataTable column names match the class properties names and that's about it.
Dynamic tables in Excel are the tables where when a new value is inserted into it. As a result, the table adjusts its size by itself. To create a dynamic table in Excel, we have two different methods: making a table of the data from the table section while another using the offset function.
You can create custom dynamic objects by using the classes in the System. Dynamic namespace. For example, you can create an ExpandoObject and specify the members of that object at run time. You can also create your own type that inherits the DynamicObject class.
This article shows 3 ways to convert a DataTable to a List in C#. This article explains various ways to convert a DataTable to a List in C#. There are the following 3 ways to convert a DataTable to a List. Using a Loop. Using LINQ. Using a Generic Method. For this example I am creating a simple Student class like:
Previous suneelsarraf codes only generated runtime dynamic object with property as string. following update will generate each property based on DataTable column's data type. Show activity on this post. There are some ORMs that can read directly from DB to dynamic ExpandoObject.
There are proper ways to construct the relationship between different fields but give the class definition is done, it can be easily done by Newtonsoft.Json Process: DataSet/DataTable (Serialize) ==> Json (Deserialize) ==> Target Object List In this example as the OP, simply do: Now the DataTable is serialized into a plain string. Then do this:
Process: DataSet/DataTable (Serialize) ==> Json (Deserialize) ==> Target Object List In this example as the OP, simply do: Now the DataTable is serialized into a plain string. Then do this: List<Class1> clslist = JsonConvert.DeserializeObject<List<Class1>> (serialized, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
class Program { static void Main() { var dt = new DataTable(); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Rows.Add(1, "x"); dt.Rows.Add(2, "y"); List<dynamic> dynamicDt = dt.ToDynamic(); Console.WriteLine(dynamicDt.First().ID); Console.WriteLine(dynamicDt.First().Name); } } public static class DataTableExtensions { public static List<dynamic> ToDynamic(this DataTable dt) { var dynamicDt = new List<dynamic>(); foreach (DataRow row in dt.Rows) { dynamic dyn = new ExpandoObject(); dynamicDt.Add(dyn); foreach (DataColumn column in dt.Columns) { var dic = (IDictionary<string, object>)dyn; dic[column.ColumnName] = row[column]; } } return dynamicDt; } }
How about with DynamicObject
:
public static class DataTableX { public static IEnumerable<dynamic> AsDynamicEnumerable(this DataTable table) { // Validate argument here.. return table.AsEnumerable().Select(row => new DynamicRow(row)); } private sealed class DynamicRow : DynamicObject { private readonly DataRow _row; internal DynamicRow(DataRow row) { _row = row; } // Interprets a member-access as an indexer-access on the // contained DataRow. public override bool TryGetMember(GetMemberBinder binder, out object result) { var retVal = _row.Table.Columns.Contains(binder.Name); result = retVal ? _row[binder.Name] : null; return retVal; } } }
You could also try overriding TrySetMember
if you wish to make the dynamic-row writable.
Usage:
DataTable table = ... var dynamicTable = table.AsDynamicEnumerable(); var firstRowsNameField = dynamicTable.First().Name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With