MS Dynamics - ConditionOperator.In 的 QueryExpression 没有结果但适用于 ConditionOperator.Equal

MS Dynamics - QueryExpression with ConditionOperator.In result in no result but works with ConditionOperator.Equal

我正在尝试进行查询以检索包含字符串列表中的文本之一的所有记录。

    QueryExpression query = new QueryExpression("account")
                {
                    ColumnSet = new ColumnSet("primarycontactid", "new_text"),
                    NoLock = true,
                    Criteria =
                    {
                        Conditions =
                        {
                            new ConditionExpression()
                            {
                                AttributeName = "new_text",
                                Operator = ConditionOperator.In,
                                Values = { texts.ToArray() }
                            }
                        }
                    }
                };

此代码执行没有问题,但没有return任何记录。

我也试过下面的代码,结果是return多条记录。

    QueryExpression query = new QueryExpression("account")
                {
                    ColumnSet = new ColumnSet("primarycontactid", "new_text"),
                    NoLock = true,
                    Criteria =
                    {
                        Conditions =
                        {
                            new ConditionExpression()
                            {
                                AttributeName = "new_text",
                                Operator = ConditionOperator.Equal,
                                Values = { texts.ToArray()[0] }
                            }
                        }
                    }
                };

我也试过了,没有错误,但是没有return。

    QueryExpression query = new QueryExpression("account")
                {
                    ColumnSet = new ColumnSet("primarycontactid", "new_text"),
                    NoLock = true,
                    Criteria =
                    {
                        Conditions =
                        {
                            new ConditionExpression()
                            {
                                AttributeName = "new_text",
                                Operator = ConditionOperator.Equal,
                                Values = { texts.ToArray() }
                            }
                        }
                    }
                };

如何使用值列表进行查询?

下面的语法应该有效。

QueryExpression q = new QueryExpression("account");
q.Criteria.AddCondition("new_text", ConditionOperator.In, new object[] { "value1", "value2" });

替代版本:

q.Criteria.AddCondition("new_text", ConditionOperator.In, "value1", "value2");

Read more

这是另一种方法。

将您的文本作为列表,然后将其转换为逗号分隔的字符串并在您的条件中使用此字符串

IList texts = new List{"1","2","testing"}; string joined = string.Join(",", texts);

那你就可以像下面这样使用了

QueryExpression query = new QueryExpression("account") { ColumnSet = new ColumnSet("primarycontactid", "new_text"), NoLock = false, Criteria = { Conditions = { new ConditionExpression() { AttributeName = "new_text", Operator = ConditionOperator.In, Values = { joined } } } } };