locked
ConditionExpress RRS feed

  • Question

  • Hi,

    I like to create a ConditionExpress like SQL query where clause " where (productid = 'a' and pricelevelid = 1) or (productid = 'b' and pricelevelid = 2) or (productid = 'c' and pricelevelid = 1) or (productid = 'd' and pricelevelid = 2)"

    Any thoughts?

    Thanks in advance,

    Young

    Wednesday, August 4, 2010 10:29 PM

Answers

  • You need to use filterexpressions.

    Ie:

    QueryExpression query = new QueryExpression();

    ...

    query.Criteria = new FilterExpression();

    query.Criteria.FilterOperator = LogicalOperator.Or;

     

    FilterExpression fs = new FilterExpression();

    fs.FilterOperator = LogicalOperator.And;

    ConditionExpression first = new ConditionExpression();

    ConditionExpression second = new ConditionExpression();

    fs.Conditions = new ConditionExpression[] { first, second };

     

    FilterExpression fs2 = new FilterExpression();

    fs2.FilterOperator = LogicalOperator.And;

    ConditionExpression first2 = new ConditionExpression();

    ConditionExpression second2 = new ConditionExpression();

    fs2.Conditions = new ConditionExpression[] { first2, second2 };

     

    query.Criteria.Filters = new FilterExpression[] { fs, fs2 };

     

    Wednesday, August 4, 2010 10:37 PM

All replies

  • You need to use filterexpressions.

    Ie:

    QueryExpression query = new QueryExpression();

    ...

    query.Criteria = new FilterExpression();

    query.Criteria.FilterOperator = LogicalOperator.Or;

     

    FilterExpression fs = new FilterExpression();

    fs.FilterOperator = LogicalOperator.And;

    ConditionExpression first = new ConditionExpression();

    ConditionExpression second = new ConditionExpression();

    fs.Conditions = new ConditionExpression[] { first, second };

     

    FilterExpression fs2 = new FilterExpression();

    fs2.FilterOperator = LogicalOperator.And;

    ConditionExpression first2 = new ConditionExpression();

    ConditionExpression second2 = new ConditionExpression();

    fs2.Conditions = new ConditionExpression[] { first2, second2 };

     

    query.Criteria.Filters = new FilterExpression[] { fs, fs2 };

     

    Wednesday, August 4, 2010 10:37 PM
  • I highly recommend the "FetchXml Wizard" from stunnware. The tool can help you build up your query from query designer and prepare the code (either fetchxml or queryexpression) for you.

    http://www.stunnware.com/default.aspx?area=products&group=swtools4&subarea=swtools4-download

    Not only useful to help you prepare the query, but also provide you an easy way to retrieve data from Dynamics instead of directly querying from database.

    Your question can be resolved by using two level filter condition. The first filter level is "or" condition which combines several second level filters. And each of second level filters is using "and" condition.

     

    Darren Tang

    http://dsharpdotnet.wordpress.com
    Thursday, August 5, 2010 3:31 AM