Dynamics365 实体集合转动态对象避免创建Model的一种方法

  EntityCollection entityCollection2 = OrganizationService.RetrieveMultiple(queryExpression);
                if (entityCollection2 != null && entityCollection2.Entities.Count > 0)
                {
                    JArray jarray = new JArray();
                    foreach (Entity item in entityCollection2.Entities)
                    {
                        JObject job = new JObject();
                        foreach (var attr in item.Attributes)
                        {
                            if (attr.Value is DateTime)
                            {
                                job.Add(attr.Key, new JValue(Convert.ToDateTime(attr.Value).ToString("yyyy-MM-dd")));
                                continue;
                            }
                            if (attr.Value is decimal)
                            {
                                job.Add(attr.Key, new JValue(Convert.ToDecimal(attr.Value).ToString("N2")));
                                continue;
                            }
                            if (attr.Value is EntityReference)
                            {
                                var ef = (EntityReference)attr.Value;
                                JObject _job = JObject.FromObject(new { Id = ef.Id, Name = ef.Name, LogicalName = ef.LogicalName });
                                job.Add(attr.Key, _job);
                                continue;
                            }
                            if (attr.Value is OptionSetValue)
                            {
                                var os = attr.Value as OptionSetValue;
                                job.Add(attr.Key, new JValue(os.Value));
                                continue;
                            }
                            job.Add(attr.Key, new JValue(attr.Value));
                        }
                        jarray.Add(job);
                    }
                    return new { BMSList = jarray, TotalRecordCount = entityCollection2.TotalRecordCount, PagingCookie = entityCollection2.PagingCookie, rolename = rolename };
                }

如果某些属性需要特殊处理的,可以在foreach循环的前面通过attr.key的值去特殊处理并continue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值