如何在MS CRM查询表达式中添加选项集筛选条件?

时间:2014-12-24 07:11:18

标签: dynamics-crm-2011 crm query-expressions

我有一个具有两个属性的实体LeaveType,1。Type,2。Available Days,其中Type是一个选项集,Available days是一个文本字段。我想获取所有这些LeaveType记录,其中Type ='Annual'在选项集中选择。我无法找到如何为选项设置值添加过滤器查询表达式。以下是我的进度方法:

public Entity Getleavetype(Guid LeaveDetailsId, IOrganizationService _orgService, CodeActivityContext Acontext)
        {
            QueryExpression GetLeavedetails = new QueryExpression();
            GetLeavedetails.EntityName = "sgfdhr_leavetype";
            GetLeavedetails.ColumnSet = new ColumnSet("new_type");
            GetLeavedetails.ColumnSet = new ColumnSet("new_availabledays");
            GetLeavedetails.Criteria.AddCondition("new_type", ConditionOperator.Equal,   "Annual" ); //Is this correct????
            GetLeavedetails.Criteria.AddCondition("new_employeeleavecalculation", ConditionOperator.Equal, LeaveDetailsId); //ignore this

            //((OptionSetValue)LeaveDetailsId["new_leavetype"]).Value

            EntityCollection LeaveDetails = _orgService.RetrieveMultiple(GetLeavedetails);
            return LeaveDetails[0];
        }

2 个答案:

答案 0 :(得分:5)

在您的情况下,您需要设置选项集的整数值,而不是标签。

假设Annual值为例如2,则代码为:

GetLeavedetails.Criteria.AddCondition("new_type", ConditionOperator.Equal, 2);

答案 1 :(得分:0)

您应该使用RetrieveAttributeRequest来查找OptionSet文字的int值。

在我的代码中看起来像:

private static int findParkedOptionValue(IOrganizationService service)
{
    RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
    {
        EntityLogicalName = Model.Invite.ENTITY_NAME,
        LogicalName = Model.Invite.COLUMN_STATUS,
        RetrieveAsIfPublished = false
    };

    // Execute the request
    RetrieveAttributeResponse attributeResponse =
        (RetrieveAttributeResponse)service.Execute(attributeRequest);
    var attributeMetadata = (EnumAttributeMetadata)attributeResponse.AttributeMetadata;

    // Get the current options list for the retrieved attribute.
    var optionList = (from o in attributeMetadata.OptionSet.Options
                      select new { Value = o.Value, Text = o.Label.UserLocalizedLabel.Label }).ToList();
    int value = (int)optionList.Where(o => o.Text == "Парковка")
                                .Select(o => o.Value)
                                .FirstOrDefault();
    return value;

}

https://community.dynamics.com/enterprise/b/crmmemories/archive/2017/04/20/retrieve-option-set-metadata-in-c中,您找到了一个完美的例子。