Search code examples
c#dynamics-crmmicrosoft-dynamicsdynamics-365query-expressions

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


I'm trying to make a query in order to retrieve all record containing one of the text in a string list.

    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() }
                            }
                        }
                    }
                };

This code execute without issue, but don't return any record.

I also tried the following code, which resulted in the return of multiple record.

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

I also tried, without error, but with no 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() }
                            }
                        }
                    }
                };

How can I do in order to query with a list of values ?


Solution

  • The below syntax should work.

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

    Alternate version:

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

    Read more