我是靠谱客的博主 冷静猫咪,最近开发中收集的这篇文章主要介绍dynamics 365 Entites转成DataTable DataCollection<Entity>转成DataTable,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
//怎么把entity转化成datatable
public static DataTable EntitiesToDatatable(this DataCollection<Entity> entities)
{
DataTable dataTable = new DataTable();
if (entities.Count > 0)
{
foreach (var item in entities[0].Attributes)
{
if (item.Value.ToString().Contains("EntityReference"))
{
dataTable.Columns.Add(item.Key.Replace("id", "name"), typeof(string));
}
dataTable.Columns.Add(item.Key, typeof(string));
}
foreach (Entity item in entities)
{
DataRow dr = dataTable.NewRow();
foreach (var Attributes in item.Attributes)
{
if (Attributes.Value.ToString().Contains("EntityReference"))
{
dr[Attributes.Key.Replace("id", "name")] = item.EntityGetValue(Attributes.Key, true);//通过名称赋值
}
dr[Attributes.Key] = item.EntityGetValue(Attributes.Key);//通过名称赋值
}
dataTable.Rows.Add(dr);
}
}
return dataTable;
}
public static string EntityGetValue(this Entity entity, string AttrName, bool ValueType = false)
{
string str = "";
ICollection<string> keys = entity.Attributes.Keys;
ICollection<object> Values = entity.Attributes.Values;
int keysindex = 0;
int Valuesindex = 0;
foreach (string keysitem in keys)
{
keysindex++;
if (keysitem == AttrName)
{
foreach (object Valuesitem in Values)
{
Valuesindex++;
if (keysindex == Valuesindex)
{
Type type = Valuesitem.GetType();
if (type.ToString().ToLower().Contains("boolean"))
{
Boolean S = entity.GetAttributeValue<Boolean>(AttrName);
if (S)
{
str = "是";
}
else
{
str = "否";
}
}
else if (type.ToString().ToLower().Contains("entityreference"))
{
if (ValueType)
str = entity.GetAttributeValue<EntityReference>(AttrName).Name;
else
str = entity.GetAttributeValue<EntityReference>(AttrName).Id.ToString();
}
else if (type.ToString().ToLower().Contains("optionsetvalue"))
{
if (ValueType)
str = entity.FormattedValues[AttrName];
else
str = entity.GetAttributeValue<OptionSetValue>(AttrName).Value.ToString();
}
else if (type.ToString().ToLower().Contains("guid"))
{
str = entity[AttrName].ToString();
}
else if (type.ToString().ToLower().Contains("datetime"))
{
str = DateTime.Parse(entity[AttrName].ToString()).AddHours(8).ToString();
}
else if (type.ToString().ToLower().Contains("string"))
{
str = entity[AttrName].ToString();
}
else if (type.ToString().ToLower().Contains("money"))
{
str = entity.GetAttributeValue<Money>(AttrName).Value.ToString("N");
}
else if (type.ToString().ToLower().Contains("decimal"))
{
str = entity.GetAttributeValue<decimal>(AttrName).ToString("N");
}
else if (type.ToString().ToLower().Contains("double"))
{
str = entity.GetAttributeValue<Double>(AttrName).ToString("N");
}
}
}
}
}
return str;
}
最后
以上就是冷静猫咪为你收集整理的dynamics 365 Entites转成DataTable DataCollection<Entity>转成DataTable的全部内容,希望文章能够帮你解决dynamics 365 Entites转成DataTable DataCollection<Entity>转成DataTable所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复