You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 19:42:11