65.9K
CodeProject 正在变化。 阅读更多。
Home

将 BusinessEntityCollection 转换为 DataTable

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1投票)

2009年9月14日

CPOL
viewsIcon

30841

downloadIcon

523

将 MS Dynamics CRM 4 BusinessEntityCollection 转换为 DataTable。

引言

MS Dynamics CRM 4 以包含 BusinessEntityCollection 的响应对象的形式返回数据。这段代码将这个 BusinessEntityCollection 转换为一个简单的 .NET DataTable

使用代码

对于在 BusinessEntityCollection 的每一行中找到的每个属性,它都会检查 DataTable 是否已经包含该名称的列。如果 DataTable 不包含该列,则该函数将其添加到 DataTable 并重新开始遍历当前选定的行。

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;
}

上面的代码调用了一个名为 getNameFromProperty() 的函数,该函数可以在附带的 zip 文件中找到。

© . All rights reserved.