0

I am using Entity Framework, ASP.NET Web API and there is a WinForm client that sends its data to the server or receives.

I know how to get one record and also how to get a collection of records at one time, but there maybe a lot amount of records which may cause problems. So how can I get a collection of new information one by one in the client side?

I think I can get a list of new ID's first and then getting them one by one. Is there a better approach?

any information about implementing this will be useful.

EDIT: to be clear, I mean getting a collection of information from server in client machine and not in server from database :)

Blazi
  • 991
  • 3
  • 9
  • 19
  • When in doubt, consult Mr. Skeet: http://stackoverflow.com/a/410050/534109 – Tieson T. Dec 10 '12 at 19:46
  • @TiesonT. That's fetching all of the items from the DB all at once, rather than one at a time. – Servy Dec 10 '12 at 19:47
  • 1
    @Servy I was mostly referring to the two posts that Jon linked to in his answer, since understanding how `yield` actually works is a good first step. But yes, the code in that example returns an entire set. – Tieson T. Dec 10 '12 at 20:12

2 Answers2

2

Paging is the answer. Below is a great link to paging with Entity Framework and MVC using Skip and Take. You basically specify the starting index (or "skip") and then the number of records you want to get (or "take"). Like this:

context.YOURDBSET.Skip(0).Take(10);  //Gets the first 10.
context.YOURDBSET.Skip(10).Take(10);  //Gets the next 10.
context.YOURDBSET.Skip(20).Take(10);  //Gets the next 10.

etc.etc.etc.

Here's that link: http://msdn.microsoft.com/en-us/magazine/gg650669.aspx

jmrnet
  • 548
  • 3
  • 11
0

A very common approach to this problem is to create some form of paging. For instance, the first time, just get the first 50 records from the server. Then based on some condition (could be user triggered, or automatic based on time, depends on your application) get the next set of data, record 51 - 100.

You can get creative with how you trigger getting the various pages of data. For instance, you could trigger the retrieval of data based on the user scrolling the mouse wheel if you need to show the data that way. Ultimately this depends on your scenario, but I believe paging is the answer.

Your page size could be 5 records, or it could be 500 records, but the idea is the same.

Rick B
  • 1,156
  • 10
  • 11
  • Thanks RickB. But the client doesn't know if there is new records on the server or not, also server doesn't know client has which records? – Blazi Dec 10 '12 at 20:07
  • The client needs to do the paging. The first query from the client should ask "How many records are there?". Then from that info, it can page what it needs to. If you think there could be new data since the first question was asked, ask again after some period of time. – Rick B Dec 10 '12 at 20:10
  • If you think new records will be a common scenario, add a date time parameter to your paging API. – Rick B Dec 10 '12 at 20:14