Click here to Skip to main content
Licence CPOL
First Posted 13 Sep 2009
Views 9,235
Downloads 287
Bookmarked 5 times

Convert BusinessEntityCollection to a DataTable

By | 13 Sep 2009 | Article
Convert an MS Dynamics CRM 4 BusinessEntityCollection to a DataTable.
 
Part of The SQL Zone sponsored by
See Also

Introduction

MS Dynamics CRM 4 returns data in a response object containing a BusinessEntityCollection. This code converts this BusinessEntityCollection into a simple .NET DataTable.

Using the code

For every property found in BusinessEntityCollection's each row, it checks if the DataTable already contains a column with that name. If the DataTable doesn't contain it, the function adds it to the DataTable and restarts traversing the currently selected row.

public DataTable convertBuinessEntityCollectionToDataTable(BusinessEntityCollection BEC)
{
    DataTable dt = new DataTable();
    int total = BEC.BusinessEntities.Length;
    //////////////////////////////////////////////////////////////////////////
    bool continueFlag = false;
    for (int i = 0; i < total; i++)
    {
        DataRow row = dt.NewRow();
        DynamicEntity myEntity = (DynamicEntity)BEC.BusinessEntities[i];
        for (int j = 0; j < myEntity.Properties.Length; j++)
        {
            Property pp;
            pp = myEntity.Properties[j];
            string columnName = pp.Name;
            string value = getNameFromProperty(pp);
            if (dt.Columns.IndexOf(columnName) == -1)
            {
                dt.Columns.Add(pp.Name, Type.GetType("System.String"));
                i--;
                continueFlag = true;
                break;
            }
            else
            {
                continueFlag=false;
            }
            row[columnName] = value;
        }
        if (continueFlag == true)
        {
            continue;
        }
        dt.Rows.Add(row);
    }
    /////////////////////////////////////////////////////////////////////////
    return dt;
}

The code above calls a function getNameFromProperty() which can be found inside the attached zip file.

License

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

About the Author

sohaibahmad



Pakistan Pakistan

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow is this related to: PinmemberSeishin#2:58 14 Sep '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 14 Sep 2009
Article Copyright 2009 by sohaibahmad
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid