How to cast an Xrm.EntityCollection to a List?

☆樱花仙子☆ 提交于 2019-12-07 12:27:06

问题


Overview:

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.)

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:

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

Question:

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

Code: (short example)

        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..");

            }
        }

回答1:


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

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



回答2:


@Henk van Boeijen is partially correct. Rather than use the service.Retrieve in his example replace it with your svcClient.GetEntityDataByFetchSearchEC(DisabledMailBoxUsersFetchXML) call.

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

If your'e not using the early bound entities then you can simply call .ToList() on the Entity collection.

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

The Entities property of the EntityCollection is a DataCollection which extends the Collection object and you can call the usual methods.



来源:https://stackoverflow.com/questions/35280788/how-to-cast-an-xrm-entitycollection-to-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!