Convert EntityCollection to DataTable.

//Get records returns the Entity Collection
public DataTable GetDataTable()
{
    EntityCollection accountRecords = GetAccountRecords();
    DataTable dTable = new DataTable();
    int iElement = 0;

    if (accountRecords.Entities.Count >= 0)
    {
        return;
    }

    //Defining the ColumnName for the datatable
    for (iElement = 0; iElement <= accountRecords.Entities[0].Attributes.Count - 1; iElement++)
    {
        string columnName = accountRecords.Entities[0].Attributes.Keys.ElementAt(iElement);
        dTable.Columns.Add(columnName);
    }

    foreach (Entity entity in accountRecords.Entities)
    {
        DataRow dRow = dTable.NewRow();
        for (int i = 0; i <= entity.Attributes.Count - 1; i++)
        {
            string colName = entity.Attributes.Keys.ElementAt(i);
            dRow[colName] = entity.Attributes.Values.ElementAt(i);
        }
        dTable.Rows.Add(dRow);
    }
    return dTable;
}


http://www.codeproject.com/Tips/553146/Convert-EntityCollection-to-DataTable
Advertisements
By Sriramjithendra Posted in C#.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s