Click here to Skip to main content
11,928,919 members (49,983 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked

Convert EntityCollection to DataTable

, 26 Feb 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Code for converting the EntityCollection of a DataTable.


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.

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

    if (accountRecords.Entities.Count >= 0)

    //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);

    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);
    return dTable;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Thakkar Amit
Program Manager
United States United States
Amit Thakkar is having total work experience over 13 years in implementing projects using various Microsoft Technologies like MS CRM 2011/4.0,, C#, MOSS 2007, SSRS and SSIS.

You may also be interested in...

Comments and Discussions

BugLogic and Syntax Error Pin
Ryan Daniels19-Mar-15 8:30
memberRyan Daniels19-Mar-15 8:30 
SuggestionSame idea generalised to IEnumerable on MSDN Blog Pin
Jerome Vibert27-Feb-13 21:45
memberJerome Vibert27-Feb-13 21:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 26 Feb 2013
Article Copyright 2013 by Thakkar Amit
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid