Home Ask Login Register

Developers Planet

Your answer is one click away!

Brian J February 2016

How to cast an Xrm.EntityCollection to a List?


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


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' /        


Henk van Boeijen February 2016

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)
    .Select(e => e.ToEntity<SystemUser>())

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))
    .Select(e => e.ToEntity<SystemUser>())

and alternatively this should work too:

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

Clayton Page February 2016

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

Post Status

Asked in February 2016
Viewed 2,889 times
Voted 11
Answered 2 times


Leave an answer

Quote of the day: live life