0

我有这个返回结果的 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);
    }
4

1 回答 1

1

Linq 和实体框架都没有任何默认的“每页”记录数。但由于您的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);
于 2013-04-07T13:15:01.037 回答