2

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

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];
        }
4

2 回答 2

5

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

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

GetLeavedetails.Criteria.AddCondition("new_type", ConditionOperator.Equal, 2);
于 2014-12-24T07:24:46.023 回答
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你找到了一个完美的例子。

于 2018-02-02T05:12:30.060 回答