如何将Xrm.EntityCollection强制转换为列表? [英] How to cast an Xrm.EntityCollection to a List?

查看:81
本文介绍了如何将Xrm.EntityCollection强制转换为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述:

我正在编写FetchXML查询,以返回Dynamics 2015在线CRM实例中具有禁用邮箱的用户。现在进入一个阶段,其中查询结果需要绑定到ListView。 (该项目正在使用 Dynamics SDK 2015库 )。

I'm coding a FetchXML query to return users with disabled mailboxes in a Dynamics 2015 online CRM instance. Now I've come to a stage where the query results need to be bound to a ListView. (The project is using the Dynamics SDK 2015 libs.)

为此,我尝试投射返回的结果,该结果是 EntityCollection ->到列表。但是 CRMSDKTypeProxy 在我的演员表代码中找不到。

In order to do this I've tried to cast the returned result which is an EntityCollection -> to a list. But the CRMSDKTypeProxy can't be found in my code for the cast.

我正在按照此示例的第二个答案进行操作投射:

I was following this example's second answer in order to do the casting:

将实体集合转换为Ilist,其中实体集合未实现IEnumerable

问题:

有人知道如何引用CRMSDKTypeProxy吗?还是将收藏集投射到列表的任何其他方法?

Does anyone know how to reference the CRMSDKTypeProxy? Or any alternative way to cast my collection to a list?

代码:(简短示例)

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 对象。)

You can use the ToEntity<T> method to do the conversion to a strong typed entity like this: (In this snippet service is an object implementing the IOrganizationService interface and query is a QueryExpression object.)

// 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();

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

I noticed you are using the CrmServiceClient in the Microsoft.Xrm.Tooling.Connector namespace. This was introduced in Dynamics CRM 2013.

您的代码可能看起来像这样:

Your code could look like this:

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

另外,这也应该起作用:

and alternatively this should work too:

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

这篇关于如何将Xrm.EntityCollection强制转换为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆