Как добавить критерий набора параметров в MS CRM Query Expression?

У меня есть сущность LeaveType с двумя атрибутами: 1. Тип, 2. Доступные дни, где Тип - это набор опций, а Доступные дни - текстовое поле. Я хочу получить все такие записи 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 ответа

В ваших условиях вам нужно установить целочисленное значение набора параметров, а не метку.

При условии, что Annual значение для примера 2, код будет:

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

Вы должны использовать RetrieveAttributeRequest найти значение int OptionSet текст.

В моем коде это выглядит так:

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 вы нашли прекрасный пример.

Другие вопросы по тегам