0

我敢肯定这已经被回答过,但似乎无法找到它。

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

例如

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

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

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

数据来自 SQL 或 Oracle 通过 EF4。

4

2 回答 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); }
    }
}
于 2012-11-07T04:47:54.663 回答
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;
        }
    }
}
于 2012-11-07T04:52:46.043 回答