web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Queryexpression - Filters propert in FilterExpression class

(0) ShareShare
ReportReport
Posted on by 205

Hello All,

I have the below code.

QueryExpression queryexp = new QueryExpression();
queryexp.EntityName = "account";
queryexp.ColumnSet.AddColumn("name");

queryexp.Criteria = new FilterExpression
{
Filters={
new FilterExpression{FilterOperator = LogicalOperator.And, Conditions={new ConditionExpression("name", ConditionOperator.Contains, "a"),
new ConditionExpression("statecode", ConditionOperator.Equal, 0) } },
new FilterExpression{FilterOperator = LogicalOperator.Or, Conditions={new ConditionExpression("name", ConditionOperator.Contains, "b"),
new ConditionExpression("name", ConditionOperator.Contains, "c") } }
}
};

From the definition of FilterExpression class Filters property is a private property without a setter. My question is how is it possible to assign values to Filters property without a setter. My above code gets compiled file without any errors, but i wanted to how is it possible.

If the above code works, then even this should work:

queryexp.Criteria.Filters= {
new FilterExpression{FilterOperator = LogicalOperator.And, Conditions={new ConditionExpression("name", ConditionOperator.Contains, "a"), new ConditionExpression("statecode", ConditionOperator.Equal, 0) } },
new FilterExpression{FilterOperator = LogicalOperator.Or, Conditions={new ConditionExpression("name", ConditionOperator.Contains, "b"),
new ConditionExpression("name", ConditionOperator.Contains, "c") } }
};

PS: This might be more of a C# question, but i still thought to ask in this forum as i felt people here would understand my question better. Also i am new to C#.

*This post is locked for comments

I have the same question (0)
  • Temmy Wahyu Raharjo Profile Picture
    2,916 on at

    Hi,

    For me the most important about code is about readability. So this is how I structure the query expression in my code:

     var query = new QueryExpression("systemuser")
                {
                    ColumnSet = new ColumnSet(false)
                };
                query.Criteria.AddCondition("isdisabled", ConditionOperator.Equal, false);
                query.Criteria.AddCondition("fullname", ConditionOperator.NotEqual, "INTEGRATION");
                query.Criteria.AddCondition("fullname", ConditionOperator.NotEqual, "SYSTEM");
    
                var tmLink = query.AddLink("teammembership", "systemuserid", "systemuserid");
                tmLink.Columns = new ColumnSet(false);
                tmLink.EntityAlias = "tm";
                tmLink.JoinOperator = JoinOperator.Inner;
                tmLink.LinkCriteria.AddCondition("teamid", ConditionOperator.Equal, teamId);
    
                var result = Context.SystemService.RetrieveMultiple(query);
                return result.Entities.Any()
                    ? result.Entities.Select(e => e.ToEntity<SystemUser>()).ToArray()
                    : new SystemUser[] { };


    For FilterExpression, I always use that one instead of creating new variable. More simple in my opinion.

  • Suresh_Satti Profile Picture
    205 on at

    Thank you for your reply and suggestion. But the question is still unanswered :)

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi,

    You are correct that the filters property is read only (private set).

    Not really sure what happens in the,. background, but when you are adding Filters, even using your code above you are technically adding it using the AddFilter method. You are not modifying the Filter itself, just the contents of the collection of the filters.

    Per Microsoft:

    This property is read only. Use the AddFilter method to change the contents of the array list.

    Basically, what you are doing is this:

               FilterExpression filter = new FilterExpression();

               filter.FilterOperator = LogicalOperator.And;

               filter.AddCondition(new ConditionExpression("name", ConditionOperator.Equal, "b"));

               QueryExpression query = new QueryExpression();

               query.Criteria.AddFilter(filter);

    You are not modifying the Filter Expression directly, just adding the Filter to the Query Expression using AddFilter and passing a FilterExpression to it.

    Hope this helps.

  • Suggested answer
    Michael Holmes Profile Picture
    10 on at

    From a C# perspective (for anyone who finds themselves here in the future), when you call the initializer syntax on the "Filters" property, you aren't actually setting the "Filters" property, which is of type base type "Collection" (derived as DataCollection). Collections implement a feature of C# Spec 3.0 that will allow the "Collection Initializer" syntax to be invoked if A) They implement IEnumerable and B) Implement the Add() method. which the Collection class does.

    For more info see this other, similar/generalised question: stackoverflow.com/.../collection-initialization.

  • Michael Holmes Profile Picture
    10 on at

    TLDR; you aren't setting the Filter's property, you're calling a Collection Initializer on the internally-initialized DataCollection<T> object using C# shorthand.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans