Dynamics中获取实体行计数时如何过滤虚拟实体?
过滤Dynamics虚拟实体以获取实体行计数
我尝试用代码获取Dynamics中所有实体的行计数,但在调用RetrieveTotalRecordCountRequest时触发错误:Entity xxx is a virtual entity, which is not supported。
原实现代码如下:
var list = new List<MyEntityData>(); // 请求获取所有实体 var entityRequest = new RetrieveAllEntitiesRequest() { EntityFilters = EntityFilters.Entity, RetrieveAsIfPublished = true }; // 执行请求 var entityResponse = (RetrieveAllEntitiesResponse)Dynamics._service.Execute(entityRequest); // 请求获取实体行计数 var request = new RetrieveTotalRecordCountRequest { EntityNames = entityResponse.EntityMetadata.Where(x => x.CanCreateForms.Value == true) .Select(x => x.LogicalName).ToArray() }; // 执行请求(此处报错) var response = (RetrieveTotalRecordCountResponse)Dynamics._service.Execute(request); // 遍历结果封装数据 foreach (var item in response.EntityRecordCountCollection) { list.Add(new MyEntityData() { name = item.Key, rows = (int)item.Value }); }
我尝试过通过设置RetrieveAsIfPublished = true,以及用.Where(x => x.CanCreateForms.Value == true)过滤实体,但都无法排除虚拟实体。
有效过滤方案
虚拟实体的ExternalCollectionName属性必然存在值(对应外部数据源的集合名称),而非虚拟实体该属性为空。通过判断该属性是否为空即可过滤掉虚拟实体,代码如下:
entityResponse.EntityMetadata.Where(x => string.IsNullOrEmpty(x.ExternalCollectionName)).Select(y => y.LogicalName)
内容的提问来源于stack exchange,提问作者VDWWD
相关产品推荐
相关产品推荐

