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