How to cast an Xrm.EntityCollection to a List?

你。 提交于 2019-12-05 21:19:40

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

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

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