Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

query entity records which are not in other collection ?

Posted on by 235

I have some records returned by a fetchxml.

I need to query same entity's records for user but exclude records got in above step via fetchxml.

 

Can you please guide how to do it ?  I have tried Linq but probably Linq don't support Not-in and Contains with CRM

 var resutl3 =Context.CreateQuery("contract")
                .Where(a => !results2.Entities.Contains(a.Attributes["contractid"]) ).ToList() ;

Kindly guide how I can write such a query.

Thanks

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: query entity records which are not in other collection ?

    Hey,

    There is a request that can convert a FetchXml request to a QueryExpression, see here: msdn.microsoft.com/.../hh547457.aspx

    There is a builder for FetchXml queries: http://fxb.xrmtoolbox.com/

    I would suggest to use this only for analysing how the corresponding query expression would look like, not for production use. You could of course just use plain FetchXml, but I would recommend to learn using QueryExpressions for the long term, since they are effective and the Visual Studio Intellisense helps you a lot, which it doesn't for FetchXml.

    Kind Regards,

    Florian

  • umma Profile Picture
    umma 235 on at
    RE: query entity records which are not in other collection ?

    Thanks Florian, but for QueryExpression is there some aid available ? Like we have Linq Pad for Linq and Advance search and some other tools to generate QueryExpressions ?

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: query entity records which are not in other collection ?

    Hey there,

    I like using CrmContext most when working early bound using classes generated by the CrmSvcUtil (msdn.microsoft.com/.../gg327844.aspx). If there are complex queries that include referenced entities however, QueryExpressions are more flexible. In most other cases I also use QueryExpressions, since they are flexible and good to use. I like FetchXml most when I want to be able to "stringify" my queries for configuration purposes.

    Your safest bet imho are QueryExpressions. The OrganizationServiceContext is nice, especially for querying, however it has some specialities that you have to get used to (tracking and so on) and updating records is often easier using the Organization service.

    Nice post regarding queries: www.inogic.com/.../fetchxml-vs-queryexpression

    Kind Regards,

    Florian

  • umma Profile Picture
    umma 235 on at
    RE: query entity records which are not in other collection ?

    Thanks Florian, apprecaite your reply. One more thing please is there a guidance for a newbie like me abut when to use Linq or FetchXml or Query Expression ? Kindly share your thoughts.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: query entity records which are not in other collection ?

    Hey there,

    You're right in that the CRM context does not support In / Not In queries. This is however a pure CRM context limitation, Linq itself can handle that perfectly well.

    If you have some other filter criteria, you could just apply those filters and call ToList afterwards. After that you can apply your contains / contains not filter, as you have already retrieved the records with ToList, so that all following filters will be applied locally.

    So this would execute without errors:

    var result = context.CreateQuery("contract")
    .ToList()
    .Where(a => !results2.Entities.Contains(a.Attributes["contractid"]) )
    .ToList();

    However if you don't have prefilters, like above, this is not a good idea performance wise, as it will retrieve all contacts from CRM, which can be quite a lot. In this case, either use a QueryExpression in combination with RetrieveMultiple, or FetchXml.

    Kind Regards,

    Florian

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans