Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

EF6 Repository - paging problem

$
0
0

Hi guys, 

I need help, because I have no idea how to solve it. I need to store "recipeId" ,line (string sortOrder = "recipeId"), in GetAsync in something else, not in string, but I have no idea in what and how. Could you please help because I spent 2 days and still nothing. Thanks a lot!

This is the code:

using System;
using AutoMapper;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using OnlineCookbook.Repository.Common;
using OnlineCookbook.DAL.Models;
using OnlineCookbook.Model.Common;
using OnlineCookbook.Model;


namespace OnlineCookbook.Repository
{
public class RecipeRepository : IRecipeRepository
{

protected IRepository Repository { get; private set; }

public RecipeRepository(IRepository repository)
{
Repository = repository;
}

private IUnitOfWork CreateUnitOfWork()
{
return Repository.CreateUnitOfWork();
}


public virtual async Task<List<IRecipe>> GetAsync(string sortOrder = "recipeId", int pageNumber = 1, int pageSize = 20)
{
try
{
//DAL list
List<Recipe> page; //return page
pageSize = (pageSize > 20) ? 20 : pageSize;

switch (sortOrder)
{
case "recipeId":
page = await Repository.WhereAsync<Recipe>()
.OrderBy(item => item.Id)
.Skip<Recipe>((pageNumber - 1) * pageSize)
.Take<Recipe>(pageSize) //whole page
.ToListAsync<Recipe>();
break;
default:
throw new ArgumentException("Wrong sorting!");
}
return new List<IRecipe>(Mapper.Map<List<RecipePOCO>>(page));//mapping from model to dal
}
catch (Exception e)
{
throw e;
}
}


Viewing all articles
Browse latest Browse all 4850


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>