检索实体清单 [英] Retrieving list of Entities
问题描述
在CRM 2011中,我可以使用EarlyBoundEntities进行常规的创建,更新,删除操作.但是,我似乎找不到使用相同类型的代码检索实体列表的示例.有没有办法使用EarlyBoundEntities检索类型的项目列表?
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 the same type of code. Is there a way of retrieving a list of items of type using EarlyBoundEntities?
我已经查看了MSDN,如果我已经知道它的GUID,我所能找到的就是如何检索它.
I've looked through MSDN and all I can find is how to retrieve an Entity if I know its GUID already.
// Retrieve the account containing several of its attributes.
ColumnSet cols = new ColumnSet(
new String[] { "name", "address1_postalcode", "lastusedincampaign" });
Account retrievedAccount = (Account)_service.Retrieve("account", _accountId, cols);
Console.Write("retrieved, ");
例如,我如何获取所有没有电话号码的帐户的列表?
How would I for example get a list of all accounts which didnt have a phone number?
推荐答案
如果已经使用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")
});
这篇关于检索实体清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!