Algorithm/pattern for selecting sub-collections using LINQ and C?

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 GetPage(List docList, List 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 : pageBreaksrequestedPage-1; int lastLine = requestedPage == lastPage? DocList.

Count : pageBreaksrequestedPage; //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 pages = Enumerable. Repeat(0, 1) . Concat(pageBreaks) .

Select ( (p, i) => new Page() { PageNumber = i, Lines = docList. GetRange(p, ((i! = pageBreaks.

Count)? PageBreaksi : docList. Count) - p) } ).

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 GetPage(List docList, List 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 : pageBreaksrequestedPage-1; int lastLine = requestedPage == lastPage?DocList.

Count : pageBreaksrequestedPage; //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 pages = Enumerable. Repeat(0, 1) .

Concat(pageBreaks) . Select ( (p, i) => new Page() { PageNumber = i, Lines = docList. GetRange(p, ((i!

= pageBreaks. Count)? PageBreaksi : docList.

Count) - p) } ).

– Joel Mueller Oct 26 '08 at 21:08 GetRange leverages the internal implementation of List. Skip/Take uses the contract of IEnumerable (which involves enumerating). If the List has a million elements in it, you'll get to elements 950,000 through 950,010 faster using GetRange.

– David B Oct 26 '08 at 23:18 "pageBreaks. Count! = 0" Do you mean pageBreaks.

Count == 0? PageBreaks.Any() is generally preferable: it doesn't require counting. – Jay Bazuzi Oct 27 '08 at 5:45 .

Count is a property on List(T) that does not require counting (also does not require allocation of an Enumerator). I think you mean Enumerable.Count() which is a method I'm not using in this post. – David B Oct 27 '08 at 14:51.

Not sure what the list of page breaks is for. I would think of it this way. A collection of strings, a page number, and the size of the page.

Then you could do something like: List strings = ... int pageNum = ... int pageSze = ... if (pageNum 1) { sizes. Take( pageNum - 2).Sum(); if (pageNum Skip( itemsToSkip ). Take( itemsToTake ).

Yep, was going to answer this. The paging pattern in Linq is implemented by the Skip and Take methods. If I is the number of items in a page, and j is the number of the page you wish to view, you would skip I * (j-1) items and take i.

– Will? Oct 26 '08 at 18:07 That's a great answer but the number of lines per page can vary on each page so the page break collection is an index into where each line that starts a new page is. – Guy Oct 26 '08 at 18:55 This variable page solution has some merit.

First page is bugged and an assignment is missing. – David B Oct 27 '08 at 0:31.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions