public DataTable convertEntityCollectionToDataTable(EntityCollection BEC)
{
DataTable dt = new DataTable();
int total = BEC.Entities.Count;
for (int i = 0; i < total; i++)
{
DataRow row = dt.NewRow();
Entity myEntity = (Entity)BEC.Entities[i];
var keys= myEntity.Attributes.Keys;
foreach (var item in keys)
{
string columnName = item;
string value = getValuefromAttribute(myEntity.Attributes[item]);
if (dt.Columns.IndexOf(columnName) == -1)
{
dt.Columns.Add(item, Type.GetType("System.String"));
}
row[columnName] = value;
}
dt.Rows.Add(row);
}
return dt;
}
private string getValuefromAttribute(object p)
{
if (p.ToString() == "Microsoft.Xrm.Sdk.EntityReference")
{
return ((EntityReference)p).Name;
}
if (p.ToString() == "Microsoft.Xrm.Sdk.OptionSetValue")
{
return ((OptionSetValue)p).Value.ToString();
}
if (p.ToString() == "Microsoft.Xrm.Sdk.Money")
{
return ((Money)p).Value.ToString();
}
if (p.ToString() == "Microsoft.Xrm.Sdk.AliasedValue")
{
return ((Microsoft.Xrm.Sdk.AliasedValue)p).Value.ToString();
}
else
{
return p.ToString();
}
}
No comments:
Post a Comment