Common code
代表段库
Csharp
查询
1.简单查询
var query = new QueryExpression("account");
var condition = new ConditionExpression("name", ConditionOperator.Equal, "Contoso");
query.Criteria.AddCondition(condition);
var order = new OrderExpression("accountnumber", OrderType.Ascending);
query.Orders.Add(order);
query.ColumnSet.AddColumns("name", "telephone1");
var results = _service.RetrieveMultiple(query);
foreach(Entity entity in results.Entities)
{
Console.WriteLine("Name: {0}", entity.GetAttributeValue<string>("name"));
}
2.多实体联合查询(并查询)
var query = new QueryExpression("account");
var link1 = query.AddLink("contact", "primarycontactid", "contactid");
link1.LinkCriteria.AddCondition("address1_city", ConditionOperator.Equal, "Seattle");
var link2 = link1.AddLink("lead", "leadid", "originatingleadid");
link2.LinkCriteria.AddCondition("statecode", ConditionOperator.Equal, 0);
query.ColumnSet.AddColumns("name", "emailaddress1");
var results = _service.RetrieveMultiple(query);
foreach (Entity entity in results.Entities)
{
Console.WriteLine("Name: {0}", entity.GetAttributeValue<string>("name"));
Console.WriteLine("Email: {0}", entity.GetAttributeValue<string>("emailaddress1"));
}
3.或查询
var query = new QueryExpression("account");
var nameCondition = new ConditionExpression("name", ConditionOperator.Like, "%b%");
var cityCondition = new ConditionExpression("address1_city", ConditionOperator.Like, "%c%");
var filter = new FilterExpression(LogicalOperator.Or);
filter.AddCondition(nameCondition);
filter.AddCondition(cityCondition);
query.Criteria.AddFilter(filter);
query.ColumnSet.AddColumns("name", "address1_city", "telephone1");
var results = _service.RetrieveMultiple(query);
foreach (Entity entity in results.Entities)
{
Console.WriteLine("Name: {0}", entity.GetAttributeValue<string>("name"));
Console.WriteLine("City: {0}", entity.GetAttributeValue<string>("address1_city"));
Console.WriteLine("Phone: {0}", entity.GetAttributeValue<string>("telephone1"));
}
获取用户安全角色
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
public static EntityCollection GetRolesForUser(IOrganizationService service, Guid systemUserId)
{
var query = new QueryExpression("role");
query.Criteria.AddCondition("businessunitid", ConditionOperator.Null);
query.Criteria.AddCondition("ismanaged", ConditionOperator.Equal, false); //选择非管理角色
var linkEntity = new LinkEntity("role", "systemuserroles", "roleid", "roleid", JoinOperator.Inner);
linkEntity.LinkCriteria.AddCondition("systemuserid", ConditionOperator.Equal, systemUserId);
query.LinkEntities.Add(linkEntity);
var result = service.RetrieveMultiple(query);
return result;
}
标签:code,name,entity,AddCondition,Common,new,var,query
From: https://www.cnblogs.com/gongdonghai/p/17317598.html