FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DNN.UserVoice/Extensions/IQueryableExtensions.cs at develop · DNNCommunity/DNN.UserVoice · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DNNCommunity
/
DNN.UserVoice
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
0
Code
Issues
8
Pull requests
10
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
DNN.UserVoice
/
Extensions
/
IQueryableExtensions.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
66 lines (59 loc) · 2.91 KB
Breadcrumbs
DNN.UserVoice
/
Extensions
/
IQueryableExtensions.cs
Copy path
File metadata and controls
66 lines (59 loc) · 2.91 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// MIT License
// Copyright DNN Community
namespace
DNN
.
Modules
.
UserVoice
.
Common
.
Extensions
{
using
DNN
.
Modules
.
UserVoice
.
Data
.
Repositories
;
using
System
;
using
System
.
Data
.
Entity
;
using
System
.
Linq
;
using
System
.
Linq
.
Expressions
;
using
System
.
Threading
;
using
System
.
Threading
.
Tasks
;
/// <summary>
/// A collection of extension methods for enumerables.
/// </summary>
public
static
class
IQueryableExtensions
{
/// <summary>
/// Orders the collection with an option to do it ascending or descending.
/// </summary>
/// <typeparam name="T">The type of items in the collection.</typeparam>
/// <typeparam name="TKey">The key to use for ordering.</typeparam>
/// <param name="source">The source collection to order.</param>
/// <param name="selector">A function to define the sorting expression.</param>
/// <param name="descending">If ture, will sort in descending order instead of ascending.</param>
/// <returns>A new enumerable sorted as specified.</returns>
public
static
IOrderedQueryable
<
T
>
Order
<
T
,
TKey
>
(
this
IQueryable
<
T
>
source
,
Expression
<
Func
<
T
,
TKey
>
>
selector
,
bool
descending
)
{
if
(
descending
)
{
return
source
.
OrderByDescending
(
selector
)
;
}
return
source
.
OrderBy
(
selector
)
;
}
/// <summary>
/// Asynchronously creates a paged list from the specified query by retrieving the items for the given page and
/// page size.
/// </summary>
/// <typeparam name="T">The type of the elements in the source query.</typeparam>
/// <param name="query">The source query to paginate. Must not be null.</param>
/// <param name="page">The one-based index of the page to retrieve. Values less than 1 are treated as 1.</param>
/// <param name="pageSize">The number of items per page. Values less than 1 are treated as 1.</param>
/// <param name="token">A cancellation token that can be used to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a PagedList{T} with the items
/// for the specified page and page size, along with pagination metadata.</returns>
public
static
async
Task
<
PagedList
<
T
>
>
ToPagedListAsync
<
T
>
(
this
IQueryable
<
T
>
query
,
int
page
,
int
pageSize
,
CancellationToken
token
=
default
)
{
page
=
Math
.
Max
(
1
,
page
)
;
pageSize
=
Math
.
Max
(
1
,
pageSize
)
;
var
total
=
await
query
.
CountAsync
(
token
)
;
var
skip
=
(
page
-
1
)
*
pageSize
;
var
items
=
await
query
.
Skip
(
skip
)
.
Take
(
pageSize
)
.
ToListAsync
(
token
)
;
var
pageCount
=
(
total
+
pageSize
-
1
)
/
pageSize
;
return
new
PagedList
<
T
>
(
items
,
page
,
pageSize
,
total
,
pageCount
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL