Add Conditions filter to QueryExpression give error: FilterExpression.Conditions has no setter

Hi,

I'm trying to receive records from a dynamics crm 4.0 tabel. Receiving the records works fine. Hoever, when I add a filter (I only need some of the records), I got an error-meassage on

filter.Conditions = new ConditionExpression[] { condition };

The tooltip-text says: "The property has 'Microsoft.Crm.Sdk.Query.FilterExpression.Conditions' no setter".
Compiling results in 2 error messages: "Property or indexer 'Microsoft.Crm.Sdk.Query.FilterExpression.Conditions' cannot be assigned to -- it is read only"  and "Cannot implicitly convert type 'Microsoft.Crm.Sdk.Query.ConditionExpression[]' to 'System.Collections.ArrayList' "

Any clues?
private static BusinessEntityCollection GetCampaigns(bool toekomst)
        {

            var query = new QueryExpression();
            query.ColumnSet = new AllColumns();
            query.EntityName = EntityName.campaign.ToString();

            // Retrieve the accounts where the last name is not Cannon.
            ConditionExpression condition = new ConditionExpression();
            condition.AttributeName = "crmrow_acc_startdatetime";
            if (toekomst)
            {
                condition.Operator = ConditionOperator.OnOrAfter;
            }
            else
            {
                condition.Operator = ConditionOperator.OnOrBefore;
            }
            condition.Values = new object[] { DateTime.Now.ToShortDateString() };

            // Build the filter based on the condition.
            FilterExpression filter = new FilterExpression();
            filter.FilterOperator = LogicalOperator.And;
            filter.Conditions = new ConditionExpression[] { condition };
            
            // Set the Criteria field.
            query.Criteria = filter;

            // set request and execute it
            var request = new RetrieveMultipleRequest();
            request.Query = query;
            request.ReturnDynamicEntities = true;

            var response = (RetrieveMultipleResponse)CrmUtil.Service.Execute(request);
            return response.BusinessEntityCollection;
        }

Open in new window

Leonard4pAsked:
Who is Participating?
 
Bob LearnedCommented:
While I am certainly not the CRM expert, I believe that what you are looking for is the FilterExpression.AddCondition method:

FilterExpression.AddCondition Method (Overloaded)
http://msdn.microsoft.com/en-us/library/bb890599.aspx
0
 
Leonard4pAuthor Commented:
Thanks TheLearnedOne

The Condition filter and the FilterExpression.AddCondition Method didn't work for me, but I found a workaround, actually a very nice one.


Thanks!  

        private static BusinessEntityCollection GetCampaigns(bool toekomst)
        {
            //determine condition for xml query
            string condition = toekomst ? "next-x-years" : "last-x-years";

            //xml query: retrieve campaigns
            string fetchXml =
                string.Format(
                    @"<fetch mapping='logical'>
                    	<entity name='campaign'>
                    		<attribute name='name' />
                    		<attribute name='typecode' />
                            <order attribute='crmrow_acc_startdatetime' />
                    		<filter>
                    			<condition attribute='crmrow_acc_startdatetime' operator='{0}' value='10' />
                    		</filter>
                    	</entity>
                    </fetch>", condition);
            var response = Processor.RetrieveMultipleWithFetchXml(fetchXml);
            return response;
        }

Open in new window

0
 
Leonard4pAuthor Commented:
this wasn't actually the solution (my response with the fetchXML was), but thanks anyway theLearnedOne
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.