且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检索实体清单

更新时间:2023-02-13 12:48:59

如果已经使用servicecontextname参数生成了早期绑定的代理类,则可以使用LINQ进行查询.

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

否则,如果您仍想使用其他查询方法(例如QueryExpression),则可以使用LINQ将所有实例转换为所需的早绑定类型.

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

用法:

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