实体IQueryable与页面,搜索和排序

时间:2012-11-07 04:40:04

标签: c# linq sorting paging

我确信之前已经回答过,但似乎无法找到它。

我正在寻找一个简单的下载(或教程)来构建可以进行排序,分页和搜索的IQueryable。

e.g。

IQueryable<T> myThings , params[] searchKeyValuePairs, params[] orderKeyValuePairs, int pageSize, int pageNumber

或类似的东西。猜测它将使用Dynamic Linq。

我的要求是针对广泛相似的数据提供大量MVC3视图。 BA指定了10种数据类型中的每一种的大约20个视图......但是这些视图中的大多数只是“今日全部”和“按成本排序”类型视图。

数据来自SQL或Oracle,通过EF4。

2 个答案:

答案 0 :(得分:3)

用于分页:

public class PagingList<T> : List<T>
{
    public int PageIndex { get; set; }
    public int PageSize { get; set; }
    public int TotalCount { get; set; }
    public int TotalPages { get; set; }
    public string pagerange { get; set; }
    public int pagingrange { get; set; }

    public PagingList(IQueryable<T> data, int page, int pagesize)
    {
        PageIndex = page;
        PageSize = pagesize;
        TotalCount = data.Count();
        TotalPages = (int)Math.Ceiling(TotalCount /(double)PageSize);

        this.AddRange(data.Skip(PageIndex * PageSize).Take(PageSize));

    }
    //public void GeneratePageRange()
    //{
    //    for (int i = 1; i <= TotalPages; i++)
    //    {
    //        pagingrange = i
    //    }
    //}
    public bool HasPreviousPage
    {
        get { return (PageIndex > 0); }
    }

    public bool HasNextPage
    {
        get { return (PageIndex + 1 < TotalPages); }
    }
}

答案 1 :(得分:1)

搜索:

public static class DataYouWant
{
    /// <summary>
    /// Function which returns the Griddata for json result of jqGrid
    /// </summary>
    public static GridData Getdata<T>(ObjectSet<T> baseList,int currentPage,int rowsPerPage,
    string sortcolumn,
    string sortord,
    string searchQuery,
    string searchColumns)where T: class
    {
        var query = baseList.OrderBy("it." + sortcolumn + " " + sortord);
        string strPredicate = string.Empty;
        dynamic searchvalue = searchQuery;
        if (!string.IsNullOrEmpty(searchColumns))
        {

            var coltype = baseList.EntitySet.ElementType.Members[searchColumns].TypeUsage.EdmType;
            if (CheckIntType(coltype))
            {
                strPredicate = "it." + searchColumns + " = @" + searchColumns;
                searchvalue = Convert.ToInt32(searchQuery);
            }
            else
                strPredicate = "CONTAINS(it." + searchColumns + ",@" + searchColumns + ")";

            query = baseList.Where(strPredicate, new ObjectParameter(searchColumns, searchvalue)).OrderBy("it." + sortcolumn + " " + sortord);
        }

        var pageddata = new PagingList<T>(query, currentPage, rowsPerPage);
        return new GridData()
        {
            Page = pageddata.PageIndex + 1,
            Records = pageddata.TotalCount,
            Rows = pageddata,
            Total = pageddata.TotalPages,
            UserData = "ok"
        };
    }

    /// <summary>
    /// Checks the EdmType and 
    /// </summary>
    public static bool CheckIntType(EdmType objEdmType)
    {
        switch (objEdmType.Name)
        {
            case "Int32":
                return true;break;
            case "Int16":
                return true;break;
            case "Int64":
                return true; break;
            case "Decimal":
                return  true;break;
            default:
                return false;
                break;
        }
    }
}