实体I可通过页面、搜索和排序进行查询

问题描述 投票:0回答:2

我确信这个问题以前已经得到过回答,但似乎找不到。

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

例如

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

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

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

数据通过 EF4 来自 SQL 或 Oracle。

c# linq sorting pagination
2个回答
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
投票

搜索:

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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.