1

Microsoft provides example like the one I have copied below for creating multiple conditions for a QueryExpression. Is there way to structure a QueryExpression so that you could dynamically handle a unknown number of conditions? In Microsofts example below they use condition1, condition2 and so on... Again I'm wondering if there's a way to create a more reusable QueryExpression that can handle a variable number of conditions. I know the whole thing could be done in LINQ but I'm specifically trying to determine if it could be done with QueryExpression.

// Create the query expression and set the entity to contact.
QueryExpression query = new QueryExpression();
query.EntityName = "contact";

// Create a condition where the first name equals Joe.
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "firstname";
condition1.Operator = ConditionOperator.Equal;
condition1.Values = new string[] { "Joe" }; 

// Create another condition where the first name equals John.
ConditionExpression condition2 = new ConditionExpression();
condition2 .AttributeName = "firstname";
condition2 .Operator = ConditionOperator.Equal;
condition2 .Values = new string[] { "John" }; 
GoBeavs
  • 499
  • 2
  • 10
  • 25
  • 1
    Could you elaborate a bit about what you mean with "Is there way to structure a QueryExpression so that you could dynamically handle a unknown number of conditions". You could of course programmatically add conditions in a loop. – Thijs Kuipers Sep 26 '12 at 16:07
  • This is not a real answer, but the shorthand version of what you're doing would be: `QueryExpression query = new QueryExpression("contact"); query.Criteria = new FilterExpression(); query.Criteria.AddCondition("firstname", ConditionOperator.In, new string[] { "Joe", "John" });` – Thijs Kuipers Sep 26 '12 at 16:12
  • I'm attempting to answer your question in detail. I'm not sure If I can answer it tonight, but I'm reviewing an article http://www.albahari.com/nutshell/predicatebuilder.aspx Hopefully, you can answer your own question with the info it provides. I'll post mine after I understand it fully. It would be a big help if you could post some of the code you have tried. – John C Sep 26 '12 at 06:07
  • For instance, are you trying to pass key value pairs to a method? Do you want to pass them as strings? Or should they be objects of a type yet to be determined? How are the values being input? On a web form? Will the values on the form have conditional operators, like a drop down box indicating if it is an AND or OR operation? Will they all be AND operations? Or can there be a combination of AND--OR--Contains--Greater Than--Less Than etc... – John C Sep 26 '12 at 06:07

3 Answers3

2

So you could programmatically build QueryExpressions, which might help to streamline object creation. The only issue I would raise, is that you may find your queries are so different it is difficult to create generic functions to support them all.

In any case here is a simple example which should hopefully get you started.

    public static QueryExpression BuildQueryExpression(String entityName, ColumnSet columnSet, LogicalOperator logicalOperator, List<ConditionExpression> conditions)
    {
        QueryExpression query = new QueryExpression(entityName);
        query.ColumnSet = columnSet;
        query.Criteria = new FilterExpression(logicalOperator);
        conditions.ForEach(c => query.Criteria.AddCondition(c));
        return query;
    }

Usage:

    QueryExpression query = BuildQueryExpression("contact", new ColumnSet(true), LogicalOperator.And, new List<ConditionExpression>()
        {
            new ConditionExpression("firstname", ConditionOperator.Equal, "James" ),
            new ConditionExpression("lastname", ConditionOperator.Equal, "Wood" ),
        });
James Wood
  • 17,286
  • 4
  • 46
  • 89
  • That's much better than what i was trying to do. Elegant too. nice work. – John C Sep 26 '12 at 23:44
  • Are you using Microsoft Dynamics CRM to build the QueryExpression? I have no idea how to reference the QueryExpression Type. Can you use Microsoft Dynamics CRM without using a web service (i.e. a console app)? – John C Oct 02 '12 at 05:21
  • If you use the CRM 2011 SDK you can create a connection without a web service reference. You will still need to use web services to interact with Crm, but this can be done from a console app. – James Wood Oct 02 '12 at 08:44
0

I know this is an old question, but I'd finally found and elegant way of writing QueryExpression, and thought would be good to share with the community.

The same query could be written like so:

    FilterExpression _filter = new FilterExpression(LogicalOperator.And);
    _filter.AddCondition("firstname", ConditionOperator.Equal, "Joe");
    _filter.AddCondition("firstname", ConditionOperator.Equal, "John");

    dynamic _queryExpression = new QueryExpression {
EntityName = Contact.EntityLogicalName,
ColumnSet = new ColumnSet(true),
Criteria = _filter
    };
aplon
  • 445
  • 6
  • 24
0

LinqKit's PredicateBuilder provides a set of clean extensions to manage this type of problem.

Honorable Chow
  • 3,097
  • 3
  • 22
  • 22