繁体   English   中英

如何使用Linq在新的CRM 2011实体记录上设置“选项列表”值?

[英]How to set Option List value on new CRM 2011 Entity record with Linq?

我正在C#中创建新的实体记录。 问题是我早绑定的Xrm类期望问题列表中的整数值,但是我所拥有的只是选择列表的字符串值。

所以,这就是我想要做的。 问题是“ OptionListValue”是整数值。 你懂; 自动创建的巨大。

是我找出该特定期权价值的唯一方法吗? 如果是这样,我将使用什么API来获取它以及如何使用它? 我期望有一些Linq方法可以这样做。 但是我可能承担的太多了。

public void CreateNewContactWithOptionListValue(string lastName, string theOptionListValue)
{
    using ( var context = new CrmOrganizationServiceContext( new CrmConnection( "Xrm" ) ) )
    {
        var contact = new Contact()
        {
            LastName = lastName,
            OptionListValue = theOptionListValue // How do I get the proper integer value from the CRM?
        };
        context.Create( contact );
    }
}

不使用Web服务的方法:

  1. 为选项集生成枚举( 是您可以执行的操作)
  2. 有了枚举后,只需解析字符串值即可。 像这样:
    public void CreateNewContactWithOptionListValue(string lastName, string theOptionListValue)
    {
        using (var context = new CrmOrganizationServiceContext(new CrmConnection("Xrm")))
        {
            new_customoptionset parsedValue;
            if (!Enum.TryParse<new_customoptionset>(theOptionListValue, out parsedValue))
            {
                throw new InvalidPluginExecutionException("Unknown value");
            }
            var contact = new Contact()
            {
                LastName = lastName,
                OptionListValue = new OptionSetValue((int)parsedValue)
            };
            context.Create(contact);
        }
    }

注意选项标签中的空格,因为它们在枚举中已被删除

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM