Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Convert DataTable to Dynamic Objects Using Parallelism in C#

I have a C# function that converts a DataTable into a list of ExpandoObjects, where each object represents a row with properties dynamically assigned from the column names and values

public static IEnumerable<ExpandoObject> ToExpandoObject(this DataTable dataTable)
{
    List<ExpandoObject> dynamicDt = [];
    var splitColumnNames = dataTable.Columns.Cast<DataColumn>().Select(c => c.ColumnName.Split("_")[0]).ToArray();
    foreach (DataRow row in dataTable.Rows)
    {
        dynamic dyn = new ExpandoObject();
        dynamicDt.Add(dyn);
        var columnIndex = 0;
        foreach (DataColumn column in dataTable.Columns)
        {
            var dic = (IDictionary<string, object>)dyn;
            dic[column.ColumnName] = ConvertColumnValue(row[column], splitColumnNames[columnIndex]);
            columnIndex++;
        }
    }

    return dynamicDt;
}

I want to improve the performance of this function by utilizing parallel processing How can I modify this function to run efficiently in parallel

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

use Parallelism with PLINQ The .AsParallel() method is used to enable parallel processing of DataRow objects. This method from PLINQ process the rows can occur on multiple threads and improving performance.

public static IEnumerable<ExpandoObject> ToExpandoObjectParallelism(this DataTable dataTable)
{
    var splitColumnNames = dataTable.Columns.Cast<DataColumn>().Select(c => c.ColumnName.Split("_")[0]).ToArray();

    // Utilize PLINQ to process rows in parallel
    return dataTable.Rows.Cast<DataRow>().AsParallel().Select(row =>
    {
        
        IDictionary<string, object> expando = new ExpandoObject();

        for (var columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
        {
            var column = dataTable.Columns[columnIndex];
            expando[column.ColumnName] = ConvertColumnValue(row[column], splitColumnNames[columnIndex]);
        }

        return (ExpandoObject)expando;
    });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading