【问题标题】:How to Add an optionset filter criteria in MS CRM Query Expression?如何在 MS CRM 查询表达式中添加选项集过滤条件?
【发布时间】:2014-12-24 07:11:18
【问题描述】:

我有一个带有两个属性的实体 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];
        }

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢吉多。你也可以帮我处理这段代码 string value = ((OptionSetValue)LeaveDetails["new_leavetype"]).Value.toString(); // 我需要获取选项集的字符串值,而不是整数值。
    • 对于需要查询元数据的标签,这里举例:nishantrana.me/2010/12/24/get-value-for-optionset-in-crm-2011
    【解决方案2】:

    您应该使用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 你找到了一个完美的例子。

    【讨论】:

      猜你喜欢
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多