0

I am fetching data from server in 2 steps .

First is fetch randomly 25 records from my table and show that records to user , now i have added a button . when user click on that button i have to show next 25 records from database but i need to sure that these records doesn't contains anyone which is already been displayed . How to do this in c#

Here is the code by which i get first 25 records

var records = context.couponservice.Query().Take(25).ToList();

thanks in advance .

7
  • How you fetch 25 records randomly? Commented Feb 6, 2013 at 12:04
  • 1
    Take returns records from the start of the sequence - it is not random. Commented Feb 6, 2013 at 12:04
  • Please comment the reason that why you vote down , so that i should keep that in mind for next time . Commented Feb 6, 2013 at 12:04
  • 1
    if you go with any of the Skip() and Take() solutions below, I'd strongly advise also adding an OrderBy() Commented Feb 6, 2013 at 12:07
  • @Smartboy Can you answer to my question? Commented Feb 6, 2013 at 12:10

4 Answers 4

1

You can use .Skip(pageSize * numberOfPages) before the .Take(pageSize) to skip however many records/pages you don't want to show.

Sign up to request clarification or add additional context in comments.

Comments

1

How about

int position = 25; // Increase this for each page
var nextRecords = context.couponservice.Query().Skip(position).Take(25).ToList();

Comments

0

Try this

for(i=0;i<noOfPages;i++)
{
   var records = context.couponservice.Query().Skip(i * 25).Take(25).ToList();
}

This will give you every time new records.

Comments

0

Keep track of which items you've already fetched. For example store the ID's in a List or something. Then collect only the items from your source whose ID is not in the List. From that result you can select 25 random items.

For example:

//Your datasource
var source = new Dictionary<int, string>
{
    {1, "One"},
    {2, "Two"},
    {3, "Three"},
    {4, "Four"},
    {5, "Five"},
    {6, "Six"},
    {7, "Seven"},
    {8, "Eight"},
    {9, "Nine"},
    {10, "Ten"}
};

//The ID's of already fetched items
var taken = new List<int> { 2, 7, 3, 6 };

//Subset: all items that haven't been taken yet:
var temp = source.Where(s => taken.Contains(s.Key) == false);

From 'temp' you can select random elements now and add the ID's of those items to the List 'taken'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.