Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Select n to m records using Entity Framework C#

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 342
    Comment on it

    This is a very common requirement for Mobile app developers who use APIs to interact with the Data. e.g. You want to show the list of events in an app and you have lots of data for the call at server. If server returns you back with all the data then request may fail or the response may be very slow. To tackle this issue, we use the concept of Paging. Paging is nothing but dividing data into multiple parts and only one part is requested at a time. So if we have 100 records at server then we may have 10 pages each containing 10 records.

    Below is the example to achieve this functionality in API.

    using (var applicationDbContext = new ApplicationDbContext()) { List events = new List(); events = applicationDbContext.Event.OrderBy(e => e.CreatedDate).Skip(pageIndex * pageSize).Take(pageSize).ToList(); }

    In above example, pageSize is the number of records you want to fetch in one call to API and pageIndex is the index of page for which you want the records.

    so if you want 31 to 40 records then pageSize will be 10 and and pageIndex will be 3. applicationDbContext is the DBContext for application. Mobile app developer will pass pageSize and pageIndex parameters to the API and the above mentioned code will be written in the API code.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: