语法每页返回一些结果

时间:2013-04-07 13:09:12

标签: linq

我有这个返回结果的linq语句,我想添加每页显示的项目数量的规格。我知道默认值是每页10个,我怎么能把它改成40?

var _roster = DataCBase.StoredProcedures.GetUser<Users>(userID, r => new Users
        {
            Name = RosterColumnMap.Name(r),
            Email = RosterColumnMap.Email(r)
        });

获取用户

public virtual IEnumerable<T> GetUser<T>(int userId, Func<IDataRecord, T> modelBinder, int resultsPerPage = 10, int pageNumber = 1)
    {
        if (userId < 1)
            throw new NullReferenceException("The sectionId cannot be null, when retreiving an element");

        if (resultsPerPage < 1)
            resultsPerPage = 1;     // enforce bare minimum result set

        if (pageNumber < 1)
            pageNumber = 1;         // enforce one-based page numbering

        SqlCommand _command = new SqlCommand("dbo.GetUser");
        _command.CommandType = CommandType.StoredProcedure;
        _command.Parameters.Add(new SqlParameter { ParameterName = "userId", SqlDbType = SqlDbType.Int, Value = userId });
        _command.Parameters.Add(new SqlParameter { ParameterName = "resultsPerPage", SqlDbType = SqlDbType.Int, Value = resultsPerPage });
        _command.Parameters.Add(new SqlParameter { ParameterName = "pageNumber", SqlDbType = SqlDbType.Int, Value = pageNumber });

        return DbInstance.ExecuteAs<T>(_command, modelBinder);
    }

1 个答案:

答案 0 :(得分:1)

Linq和实体框架都没有任何默认的记录数&#39;每页&#39;。但由于您的GetUser函数包含resultsPerPage参数,因此您可以执行此操作:

var _roster = DataCBase.StoredProcedures.GetUser<Users>(userID, r => new Users
    {
        Name = RosterColumnMap.Name(r),
        Email = RosterColumnMap.Email(r)
    }, 40);

要限制Linq中的结果数量,请使用Enumerable.Take方法:

var _roster = DataCBase.StoredProcedures.GetUser<Users>(userID, r => new Users
    {
        Name = RosterColumnMap.Name(r),
        Email = RosterColumnMap.Email(r)
    }).Take(40);