Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8560165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:12:57+00:00 2026-06-11T16:12:57+00:00

Given that the CRM 2011 linq provider performs paging automatically behind the scenes. Is

  • 0

Given that the CRM 2011 linq provider performs paging automatically behind the scenes.
Is there a way to set an upper limit on the number of records fetched when a linq
query is executed (similar to setting a PagingInfo.Count on a QueryExpression for paging)

I have a scenario where I need approx 20K+ records to be pulled for an update(no I cannot and do not need to filter down the record set further). Ideally I’d prefer to use the Skip & Take operators but since Count is not supported how would you know how many records to skip and when
to stop fetching more records.

Ideally I’d like to use TPL and processes batches of say 3K or 5K records in parallel so that I can get more throughput and don’t have to block. The OrganizationserviceContext is not thread safe from what I know. Are there any good examples that illustrate how to partition the dataset in this case say using Parallel.For or Parallel.ForEach.
How would you partition and would you need to use a different context object for each parition?

Thanks.

UPDATE:
Here is what I came up with:
The idea is to get the total count of records to process and use PLINQ to farm out the processing of each subset of data across tasks using a new OrganizationServiceContext object per task.

static void Main(string[] args)
    {  
       int pagesize = 2000;
        // use FetchXML aggregate functions to get total count
        // Reference: http://msdn.microsoft.com/en-us/library/gg309565.aspx
        int totalcount = GetTotalCount();

       int totalPages = (int)Math.Ceiling((double)totalcount / (double)pagesize);            
       try
        {
            Parallel.For(0, totalPages, () => new MyOrgserviceContext(),
                (pageIndex, state, ctx) =>
                {

                    var items = ctx.myEntitySet.Skip((pageIndex - 1) * pagesize).Take(pagesize);
                    var itemsArray = items.ToArray();
                    Console.WriteLine("Page:{0} - Fetched:{1}", pageIndex, itemsArray.Length);
                    return ctx;
                },
                ctx => ctx.Dispose()
                );
        }
        catch (AggregateException ex)
        {
           //handle as needed
        }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:12:58+00:00Added an answer on June 11, 2026 at 4:12 pm

    So the way I would do this would be to keep querying the records using skip and take until I run out of records.

    Check out my example below, it uses int’s for simplicity, but the approach should still apply to Linq-to-Crm.

    So just keep performing your query, skipping previous records, taking the ones you want for that page, then counting at the end to see if you recieved a full page – if you didnt then you have run out of records.

    Code

    List<int> ints = new List<int>()
    {
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
    };
    
    int pageNumber = 0;
    int recordsPerPage = 3;
    
    while(true)
    {
        IEnumerable<int> page = ints.Where(i => i < 11).Skip(recordsPerPage * pageNumber).Take(recordsPerPage);
    
        foreach(int i in page)
        {
            Console.WriteLine(i);
        }
    
        Console.WriteLine("end of page");
        pageNumber++;
    
        if (page.Count() < recordsPerPage)
        {
            break;
        }
    }
    

    Output:

    1
    2
    3
    end of page
    4
    5
    6
    end of page
    7
    8
    9
    end of page
    10
    end of page
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We found out that Linq for CRM 2011 is horribly broken - it seems
Given that Scala support is itself a plugin in intellij, is there any way
Given that the Rails Way seems to be not to use foreign key constraints,
Given that Mark Pilgrim pulled his Dive into Greasemonkey on 2011-10-06, what is a
Given that Unicode has been around for 18 years , why are there still
a Microsoft CRM 2011 question. Given the following parameters: An entity schema name (i.e.
Given that there is a tree depicted as below: d1a--d1b---d1c--d1d--d1e <dev> / / \
Given that I know how to set the .htaccess... So my user instead of
given that I can add various attributes to the stock membership provider I assume
Given that the web application doesn't have su privileges, I'd like to execute a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.