Retrieving list of Entities

前端 未结 2 708
梦谈多话
梦谈多话 2021-01-03 09:46

In CRM 2011 I can do the usual Create, Update, Delete operations using EarlyBoundEntities. I cant, however, seem to find an example of retrieving a list of entities using t

2条回答
  •  臣服心动
    2021-01-03 09:55

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

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

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

    Usage:

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

提交回复
热议问题