Search code examples
c#dynamics-crmmicrosoft-dynamicsquery-expressions

Query particular set of records with QueryExpression


Is there a way to build a QueryExpression returning just a particular set of records?

I have the following Criteria Types:

First:
Returns the first n Records (i.e. select top)

Last:
Returns the last n records

Every:
Returns every n'th record

For the type "First" I can use

queryExpression.TopCount = number_of_records

But I have no Idea how I can achieve the other types of criteria. The issue is that there are quite big data volumes and if I need first to get all records and query the result for example with Linq to customize the resultset I will probably have a performance issue.

If I could build the QueryExpression just selecting exactly what I need the whole thing gets more efficient.

Does anybody have an idea on how to achieve this with a QueryExpression?

The system in question is Microsoft Dynamics CRM Online


Solution

  • For the "last N" you can reverse the sort and use TopCount again.

    For the "every Nth" you might want to consider paging the Query Expression.

    Say you're looking for every 10th record. What I might do would be to set my page size to 10 (query.PageInfo.Count).

    To iterate through the pages as quickly as possible I'd make my "main" query return only the GUIDs. When I retrieve a new page of GUIDs, I'd grab the first GUID and get the columns I want for that record using a separate Retrieve call.