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

11.9k views Asked by At

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 ?

2

There are 2 answers

3
Arun Vinoth-Precog Tech - MVP On BEST ANSWER

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

0
AnkUser On

Here is one more approach.

Make your texts as list and then convert it to comma separated string and use this string in your condition

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

Then you can use it as below

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