Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Queryexpression - Filters propert in FilterExpression class

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

  • Michael Holmes Profile Picture
    Michael Holmes 10 on at
    RE: Queryexpression - Filters propert in FilterExpression class

    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.

  • Suggested answer
    Michael Holmes Profile Picture
    Michael Holmes 10 on at
    RE: Queryexpression - Filters propert in FilterExpression class

    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.

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Queryexpression - Filters propert in FilterExpression class

    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.

  • Suresh_Satti Profile Picture
    Suresh_Satti 205 on at
    RE: Queryexpression - Filters propert in FilterExpression class

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

  • Temmy Wahyu Raharjo Profile Picture
    Temmy Wahyu Raharjo 2,914 on at
    RE: Queryexpression - Filters propert in FilterExpression class

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans