如何将 Xrm.EntityCollection 转换为列表?

How to cast an Xrm.EntityCollection to a List?

概览:

我正在为 return 在 Dynamics 2015 在线 CRM 实例中禁用邮箱的用户编写 FetchXML 查询。现在我已经到了需要将查询结果绑定到 ListView 的阶段。 (该项目正在使用Dynamics SDK 2015 libs。)

为了做到这一点,我尝试将 returned 结果,即 EntityCollection -> 转换为列表。但是在我的转换代码中找不到 CRMSDKTypeProxy

我按照这个例子的第二个答案进行转换:

Convert Entity Collection to Ilist where Entity Collection does not implement IEnumerable

问题:

有人知道如何引用 CRMSDKTypeProxy 吗?或将我的 collection 转换为列表的任何替代方法?

代码:(简短示例)

if (ctrl.CrmConnectionMgr != null && ctrl.CrmConnectionMgr.CrmSvc != null && ctrl.CrmConnectionMgr.CrmSvc.IsReady)
{
    CrmServiceClient svcClient = ctrl.CrmConnectionMgr.CrmSvc;
    if (svcClient.IsReady)
    {
        // Get data from CRM . 
        string DisabledMailBoxUsersFetchXML =
            @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                <entity name='systemuser'>
                <attribute name='fullname' />
                <attribute name='businessunitid' />
                <attribute name='title' />
                <attribute name='address1_telephone1' />
                <attribute name='positionid' />
                <attribute name='systemuserid' />
                <order attribute='fullname' descending='false' />
                <link-entity name='mailbox' from='mailboxid' to='defaultmailbox' alias='aa'>
                    <filter type='and'>
                    <condition attribute='statecode' operator='eq' value='1' />
                    </filter>
                </link-entity>
                </entity>
            </fetch>";

        var DisabledMailBoxUsersResult = svcClient.GetEntityDataByFetchSearchEC(DisabledMailBoxUsersFetchXML);

        if (DisabledMailBoxUsersResult != null)
        {
            //perform the cast here --->
            var disabledMailBoxUsersList = (from t in DisabledMailBoxUsersResult.Entities select t as CRMSDKTypeProxy.SystemUser).ToList();
            disabledMailboxUserLBx.ItemsSource = disabledMailBoxUsersList;
        }
        else
            MessageBox.Show("All user's mailboxes are approved..");

    }
}

您可以使用 ToEntity<T> 方法转换为强类型实体,如下所示:(在此代码段中 service 是一个实现 IOrganizationService 接口的对象,并且 query 是一个 QueryExpression 对象。)

// RetrieveMultiple will never return null, so this one-liner is safe to use.
var userList = service.RetrieveMultiple(query)
    .Entities
    .Select(e => e.ToEntity<SystemUser>())
    .ToList();

我注意到您正在使用 Microsoft.Xrm.Tooling.Connector 命名空间中的 CrmServiceClient。这是在 Dynamics CRM 2013 中引入的。

您的代码可能如下所示:

var userList = svcClient.OrganizationServiceProxy
    .RetrieveMultiple(new FetchExpression(fetchXml))
    .Entities
    .Select(e => e.ToEntity<SystemUser>())
    .ToList();

或者这也应该有效:

var userList = svcClient.GetEntityDataByFetchSearchEC(fetchXml)
    .Entities
    .Select(e => e.ToEntity<SystemUser>())
    .ToList();

@Henk van Boeijen 部分正确。而不是在他的示例中使用 service.Retrieve 将其替换为您的 svcClient.GetEntityDataByFetchSearchEC(DisabledMailBoxUsersFetchXML) 调用。

var userList = svcClient.GetEntityDataByFetchSearchEC(DisabledMailBoxUsersFetchXML).Entities.Select(e => e.ToEntity<SystemUser>());

如果您不使用早期绑定的实体,那么您只需在实体集合上调用 .ToList()。

var userList = svcClient.GetEntityDataByFetchSearchEC(DisabledMailBoxUsersFetchXML).Entities.ToList();

EntityCollection 的实体 属性 是一个扩展了 Collection 对象的 DataCollection,您可以调用通常的方法。