仅对于LINQ to Entities中的已排序输入,才支持方法'Skip' [英] The method 'Skip' is only supported for sorted input in LINQ to Entities

查看:56
本文介绍了仅对于LINQ to Entities中的已排序输入,才支持方法'Skip'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么可能导致此问题?

public ActionResult Index(int page = 0)
{
    const int pageSize = 3;
    var areas = repo.FindAllAreas();
    var paginatedArea = new PaginatedList<Area>(areas, page, pageSize);

    return View(paginatedArea);
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UTEPSA.Controllers
{
    class PaginatedList<T> : List<T>
    {
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }
        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
//ERROR HERE->>this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }
        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 0);
            }
        }
        public bool HasNextPage
        {
            get
            {
                return (PageIndex + 1 < TotalPages);
            }
        }
    }
}

有什么建议吗?

推荐答案

似乎错误恰如其名.只允许在已排序的输入上跳过".搜索此错误,我已经找到了.

Seems like the error is exactly what it is says. "Skip is only allowed on Sorted inputs". Searching for this error, I've found this.

如果您在跳过之前添加了OrderBy,则应该修复该问题:

It should be fixed if you include an OrderBy before Skip:

source.orderBy(???).Skip(PageIndex * PageSize).Take(PageSize)); 

这可能是个问题,因为要传递通用对象T.您可能需要扩展类以接收另一个参数,以按元素指示顺序.

Which might be a problem since you are passing a generic object T. You might need to expand your class to receive another parameter to indicate the order by element.

这篇关于仅对于LINQ to Entities中的已排序输入,才支持方法'Skip'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆