Skip to content

Commit 849952f

Browse files
authored
Update PagedResult logic (#349)
* Add int? rowCount to PageResult * fix
1 parent 9235fd2 commit 849952f

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1360,18 +1360,20 @@ public static IQueryable<TSource> Page<TSource>([NotNull] this IQueryable<TSourc
13601360
/// <param name="source">The IQueryable to return elements from.</param>
13611361
/// <param name="page">The page to return.</param>
13621362
/// <param name="pageSize">The number of elements per page.</param>
1363+
/// <param name="rowCount">If this optional parameter has been defined, this value is used as the RowCount instead of executing a Linq `Count()`.</param>
13631364
/// <returns>PagedResult</returns>
1364-
public static PagedResult PageResult([NotNull] this IQueryable source, int page, int pageSize)
1365+
public static PagedResult PageResult([NotNull] this IQueryable source, int page, int pageSize, int? rowCount = null)
13651366
{
13661367
Check.NotNull(source, nameof(source));
13671368
Check.Condition(page, p => p > 0, nameof(page));
13681369
Check.Condition(pageSize, ps => ps > 0, nameof(pageSize));
1370+
Check.Condition(rowCount, rc => rc == null || rc >= 0, nameof(rowCount));
13691371

13701372
var result = new PagedResult
13711373
{
13721374
CurrentPage = page,
13731375
PageSize = pageSize,
1374-
RowCount = source.Count()
1376+
RowCount = rowCount ?? source.Count()
13751377
};
13761378

13771379
result.PageCount = (int)Math.Ceiling((double)result.RowCount / pageSize);
@@ -1387,18 +1389,20 @@ public static PagedResult PageResult([NotNull] this IQueryable source, int page,
13871389
/// <param name="source">The IQueryable to return elements from.</param>
13881390
/// <param name="page">The page to return.</param>
13891391
/// <param name="pageSize">The number of elements per page.</param>
1392+
/// <param name="rowCount">If this optional parameter has been defined, this value is used as the RowCount instead of executing a Linq `Count()`.</param>
13901393
/// <returns>PagedResult{TSource}</returns>
1391-
public static PagedResult<TSource> PageResult<TSource>([NotNull] this IQueryable<TSource> source, int page, int pageSize)
1394+
public static PagedResult<TSource> PageResult<TSource>([NotNull] this IQueryable<TSource> source, int page, int pageSize, int? rowCount = null)
13921395
{
13931396
Check.NotNull(source, nameof(source));
13941397
Check.Condition(page, p => p > 0, nameof(page));
13951398
Check.Condition(pageSize, ps => ps > 0, nameof(pageSize));
1399+
Check.Condition(rowCount, rc => rc == null || rc >= 0, nameof(rowCount));
13961400

13971401
var result = new PagedResult<TSource>
13981402
{
13991403
CurrentPage = page,
14001404
PageSize = pageSize,
1401-
RowCount = Queryable.Count(source)
1405+
RowCount = rowCount ?? Queryable.Count(source)
14021406
};
14031407

14041408
result.PageCount = (int)Math.Ceiling((double)result.RowCount / pageSize);

src/System.Linq.Dynamic.Core/Validation/Check.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ namespace System.Linq.Dynamic.Core.Validation
1212
[DebuggerStepThrough]
1313
internal static class Check
1414
{
15-
[ContractAnnotation("value:null => halt")]
1615
public static T Condition<T>([NoEnumeration] T value, [NotNull] Predicate<T> condition, [InvokerParameterName] [NotNull] string parameterName)
1716
{
1817
NotNull(condition, nameof(condition));
19-
NotNull(value, nameof(value));
2018

2119
if (!condition(value))
2220
{
@@ -136,4 +134,4 @@ public static Type ValidEntityType(Type value, [InvokerParameterName] [NotNull]
136134
return value;
137135
}
138136
}
139-
}
137+
}

0 commit comments

Comments
 (0)