Retrieving list of Entities

折月煮酒 提交于 2019-11-30 15:59:42

If you've generated your early-bound proxy classes with the servicecontextname parameters, then you could LINQ for querying.

var context = new XrmServiceContext(service);
var accounts = context.AccountSet.Where(item => item.Telephone1 == null);

Otherwise, if you still wanted to use other query methods such as QueryExpression you could use LINQ to cast all instances to the desire early-bound type.

var contacts = service.RetrieveMultiple(new QueryExpression
                                            {
                                                EntityName = "contact",
                                                ColumnSet = new ColumnSet("firstname")
                                            })
    .Entities
    .Select(item => item.ToEntity<Contact>());

You could also use an extension method if you'd prefer:

public static IEnumerable<T> RetrieveMultiple<T>(this IOrganizationService service, QueryBase query) where T : Entity
{
    return service.RetrieveMultiple(query)
        .Entities
        .Select(item => item.ToEntity<T>());
}

Usage:

var contacts = service.RetrieveMultiple<Contact>(new QueryExpression
                                                        {
                                                            EntityName = "contact",
                                                            ColumnSet = new ColumnSet("firstname")
                                                        });

There's actually plenty of material in the SDK on MSDN that shows how to query an entity.

Create Queries to Retrieve Data

Build Queries with LINQ - primarily early-bound examples

The API provides three more or less equivalent ways to query the database (LINQ, FetchXml, and QueryExpression), though there are limitations (for example, see LINQ limitations) that you can only get around by using an on-premise installation and native SQL calls.

For the example of the accounts with a null phone number that you gave, though, any of the three supported query methods will work.

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