我是靠谱客的博主 冷静猫咪,这篇文章主要介绍dynamics 365 Entites转成DataTable DataCollection<Entity>转成DataTable,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//怎么把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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部