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 60561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:10:52+00:00 2026-05-10T18:10:52+00:00

I have a C# collection of strings. Each string is a sentence that can

  • 0

I have a C# collection of strings. Each string is a sentence that can appear on a page. I also have a collection of page breaks which is a collection of int’s. representing the index where the collection of strings are split to a new page.

Example: Each 10 items in the string collection is a page so the collection of page breaks would be a collection of int’s with the values of 10, 20, 30. …

So if there are 2 pages of strings then there will be 1 item in the page break collection and if there is 1 page then the page break collection would have zero items.

I am trying to create the following function:

List<string> GetPage(List<string> docList, List<int> pageBreakList, int pageNum) {     // This function returns a subset of docList - just the page requested } 

I’ve taken a few stabs at writing this function and keep on coming up with complex if and switch statements to take into account single and two page documents and page numbers being requested outside the range (e.g. last page should be returned if page number is greater than number of pages and first page if page number is 0 or less).

My struggle with this problem leads me to ask the question: Is there a well known pattern or algorithm to address this type of subset query?

  • 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. 2026-05-10T18:10:53+00:00Added an answer on May 10, 2026 at 6:10 pm

    ‘Pure’ Linq isn’t a good fit for this problem. The best fit is to rely on the methods and properties of List(T). There aren’t -that- many special cases.

    //pageNum is zero-based. List<string> GetPage(List<string> docList, List<int> pageBreaks, int pageNum) {    // 0 page case   if (pageBreaks.Count != 0)   {     return docList;   }    int lastPage = pageBreaks.Count;    //requestedPage is after the lastPage case   if (requestedPage > lastPage)   {     requestedPage = lastPage;   }     int firstLine = requestedPage == 0 ? 0  :       pageBreaks[requestedPage-1];   int lastLine = requestedPage == lastPage ? docList.Count :       pageBreaks[requestedPage];    //lastLine is excluded.  6 - 3 = 3 - 3, 4, 5    int howManyLines = lastLine - firstLine;    return docList.GetRange(firstLine, howManyLines); } 

    You don’t want to replace the .Count property with linq’s .Count() method. You don’t want to replace the .GetRange() method with linq’s .Skip(n).Take(m) methods.

    Linq would be a better fit if you wanted to project these collections into other collections:

    IEnumerable<Page> pages =   Enumerable.Repeat(0, 1)   .Concat(pageBreaks)   .Select   (     (p, i) => new Page()     {       PageNumber = i,       Lines =          docList.GetRange(p, ((i != pageBreaks.Count) ? pageBreaks[i] : docList.Count)  - p)     }   ); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There can be significant performance benefits from this, in some… May 11, 2026 at 1:33 pm
  • added an answer Add the float:left to the second DIV as well -… May 11, 2026 at 1:33 pm
  • added an answer Use synchronization between your threads ! You should look into… May 11, 2026 at 1:33 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.