locked
Problem making a FilterExpression C# RRS feed

  • Question

  • I'm trying to make FilterExpression to extract some appointments.

    I need the appointments that are created before a date OR(

    created before a date AND modified after a date)

     

    The below code is wrong but it might help explain what I'm trying to do:

     

    FilterExpression[] f2 = new FilterExpression[1];
    f2[0] = new FilterExpression(LogicalOperator.And);
    f2[0].Conditions.Add(new ConditionExpression("createdon", ConditionOperator.OnOrBefore, dateTime));
    f2[0].Conditions.Add(new ConditionExpression("modifiedon", ConditionOperator.OnOrAfter, dateTime));

    FilterExpression[] f = new FilterExpression[1];
    f[0] = new FilterExpression(LogicalOperator.Or);
    f[0].Conditions.Add(new ConditionExpression("createdon", ConditionOperator.OnOrAfter, dateTime));

    query = new QueryExpression();
    query.EntityName = EntityLogicalName;
    query.ColumnSet = new ColumnSet(true);
    query.Criteria.Filters.AddRange(f);
    query.Criteria.Filters.AddRange(f2);

    Any suggestions?

     
    Thursday, June 19, 2014 12:06 PM

All replies

  • Hi,

    Try using:

    query.Criteria.AddFilter(f);
    query.Criteria.AddFilter(f2);

    instead of:

    query.Criteria.Filters.AddRange(f);
    query.Criteria.Filters.AddRange(f2);
    


    Admin QuikView Solution for CRM 2013

    Thursday, June 19, 2014 12:10 PM
  • f2 needs to be a child filter of f.

    Try:

     
    FilterExpression[] f = new FilterExpression[1];
     f[0] = new FilterExpression(LogicalOperator.Or);
     f[0].Conditions.Add(new ConditionExpression("createdon", ConditionOperator.OnOrAfter, dateTime));
    
    FilterExpression[] f2 = new FilterExpression[1];
     f2[0] = new FilterExpression(LogicalOperator.And);
     f2[0].Conditions.Add(new ConditionExpression("createdon", ConditionOperator.OnOrBefore, dateTime));
     f2[0].Conditions.Add(new ConditionExpression("modifiedon", ConditionOperator.OnOrAfter, dateTime));
    
    f.AddFilter(f2);
     
    query = new QueryExpression();
     query.EntityName = EntityLogicalName;
     query.ColumnSet = new ColumnSet(true);
     query.Criteria.Filters.AddRange(f);
    


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, June 19, 2014 2:59 PM
    Moderator