Map entity pagination class to dto pagination class
I have the following PaginatedList class.
public class PaginatedList<T> : List<T>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<T> items, int totalRecords, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = totalRecords;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<T>> CreateAsync(
IQueryable<T> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}
I'm using this class to get pagination information for list of entity that is retrieved with EF.
This is how I use this class to return list of users with pagination info.
var users = await PaginatedList<User>.CreateAsync(userQuery, pageIndex, pageSize);
Above call will return PaginatedList<User>
object.
If I have a DTO class for that entity, let's call it UserDto
. How do I use automapper to convert PaginatedList<User>
into PaginatedList<UserDto>
so the result will have all userDto objects and also the pagination info?
Otherwise, is there another/better way to achieve something similar?
c# automapper
add a comment |
I have the following PaginatedList class.
public class PaginatedList<T> : List<T>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<T> items, int totalRecords, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = totalRecords;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<T>> CreateAsync(
IQueryable<T> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}
I'm using this class to get pagination information for list of entity that is retrieved with EF.
This is how I use this class to return list of users with pagination info.
var users = await PaginatedList<User>.CreateAsync(userQuery, pageIndex, pageSize);
Above call will return PaginatedList<User>
object.
If I have a DTO class for that entity, let's call it UserDto
. How do I use automapper to convert PaginatedList<User>
into PaginatedList<UserDto>
so the result will have all userDto objects and also the pagination info?
Otherwise, is there another/better way to achieve something similar?
c# automapper
1
Deriving fromList<T>
is an extremely poor choice. I highly recommend reading Is-A Versus Has-A.
– Erik Philips
Jan 3 at 3:16
@ErikPhilips thanks for the suggestion, I'll take a look
– muhihsan
Jan 3 at 5:55
add a comment |
I have the following PaginatedList class.
public class PaginatedList<T> : List<T>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<T> items, int totalRecords, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = totalRecords;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<T>> CreateAsync(
IQueryable<T> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}
I'm using this class to get pagination information for list of entity that is retrieved with EF.
This is how I use this class to return list of users with pagination info.
var users = await PaginatedList<User>.CreateAsync(userQuery, pageIndex, pageSize);
Above call will return PaginatedList<User>
object.
If I have a DTO class for that entity, let's call it UserDto
. How do I use automapper to convert PaginatedList<User>
into PaginatedList<UserDto>
so the result will have all userDto objects and also the pagination info?
Otherwise, is there another/better way to achieve something similar?
c# automapper
I have the following PaginatedList class.
public class PaginatedList<T> : List<T>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<T> items, int totalRecords, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = totalRecords;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<T>> CreateAsync(
IQueryable<T> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}
I'm using this class to get pagination information for list of entity that is retrieved with EF.
This is how I use this class to return list of users with pagination info.
var users = await PaginatedList<User>.CreateAsync(userQuery, pageIndex, pageSize);
Above call will return PaginatedList<User>
object.
If I have a DTO class for that entity, let's call it UserDto
. How do I use automapper to convert PaginatedList<User>
into PaginatedList<UserDto>
so the result will have all userDto objects and also the pagination info?
Otherwise, is there another/better way to achieve something similar?
c# automapper
c# automapper
edited Jan 3 at 3:00


Erik Philips
41.6k694126
41.6k694126
asked Jan 3 at 1:34
muhihsanmuhihsan
1,16851429
1,16851429
1
Deriving fromList<T>
is an extremely poor choice. I highly recommend reading Is-A Versus Has-A.
– Erik Philips
Jan 3 at 3:16
@ErikPhilips thanks for the suggestion, I'll take a look
– muhihsan
Jan 3 at 5:55
add a comment |
1
Deriving fromList<T>
is an extremely poor choice. I highly recommend reading Is-A Versus Has-A.
– Erik Philips
Jan 3 at 3:16
@ErikPhilips thanks for the suggestion, I'll take a look
– muhihsan
Jan 3 at 5:55
1
1
Deriving from
List<T>
is an extremely poor choice. I highly recommend reading Is-A Versus Has-A.– Erik Philips
Jan 3 at 3:16
Deriving from
List<T>
is an extremely poor choice. I highly recommend reading Is-A Versus Has-A.– Erik Philips
Jan 3 at 3:16
@ErikPhilips thanks for the suggestion, I'll take a look
– muhihsan
Jan 3 at 5:55
@ErikPhilips thanks for the suggestion, I'll take a look
– muhihsan
Jan 3 at 5:55
add a comment |
2 Answers
2
active
oldest
votes
Automapper is perfect for me, but are going to need to create and default constructor on the PaginatedList
class
public PaginatedList()
{
PageIndex = 0;
TotalRecords = 0;
TotalPages = 0;
}
-------------------------------- Example
class User
{
public string Name { get; set; }
}
class UserDto
{
public string NameDto { get; set; }
}
public class UserProfile: Profile
{
public UserProfile()
{
CreateMap<User, UserDto>()
.ForMember(target => target.NameDto, x => x.MapFrom(source => source.Name))
.ReverseMap();
}
}
internal class Program
{
private static void Main(string args)
{
Mapper.Initialize(config =>
{
config.AddProfile<UserProfile>();
});
var items = new List<User> { new User { Name = "First name" } };
var users = new PaginatedList<User>(items, 1, 0, 1);
var usersDtos = Mapper.Map<PaginatedList<UserDto>>(users);
}
}
add a comment |
I ended up creating another factory method within the PaginatedList which will handle the conversion.
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
Ensure.It.IsGreaterThan(0, pageIndex);
Ensure.It.IsGreaterThan(0, pageSize);
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
Then make the existing CreateAsync
method to call this new CreateAsync<TSource>
method.
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
With this, if we want to keep returning the same Entity class, we can use it like this
await PaginatedList<User>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
And if we want to convert the entity class to return a Dto class or other class, it can be used like this
await PaginatedList<UserDto>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
If we don't convert the Entity into other class, we don't need to specify anything within the automapper configuration. But if you want to map the entity into other class, then it needs to be configured within the automapper.
More or less, here is how the PaginatedList class looks like
public class PaginatedList<TResult> : List<TResult>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<TResult> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = count;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015297%2fmap-entity-pagination-class-to-dto-pagination-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Automapper is perfect for me, but are going to need to create and default constructor on the PaginatedList
class
public PaginatedList()
{
PageIndex = 0;
TotalRecords = 0;
TotalPages = 0;
}
-------------------------------- Example
class User
{
public string Name { get; set; }
}
class UserDto
{
public string NameDto { get; set; }
}
public class UserProfile: Profile
{
public UserProfile()
{
CreateMap<User, UserDto>()
.ForMember(target => target.NameDto, x => x.MapFrom(source => source.Name))
.ReverseMap();
}
}
internal class Program
{
private static void Main(string args)
{
Mapper.Initialize(config =>
{
config.AddProfile<UserProfile>();
});
var items = new List<User> { new User { Name = "First name" } };
var users = new PaginatedList<User>(items, 1, 0, 1);
var usersDtos = Mapper.Map<PaginatedList<UserDto>>(users);
}
}
add a comment |
Automapper is perfect for me, but are going to need to create and default constructor on the PaginatedList
class
public PaginatedList()
{
PageIndex = 0;
TotalRecords = 0;
TotalPages = 0;
}
-------------------------------- Example
class User
{
public string Name { get; set; }
}
class UserDto
{
public string NameDto { get; set; }
}
public class UserProfile: Profile
{
public UserProfile()
{
CreateMap<User, UserDto>()
.ForMember(target => target.NameDto, x => x.MapFrom(source => source.Name))
.ReverseMap();
}
}
internal class Program
{
private static void Main(string args)
{
Mapper.Initialize(config =>
{
config.AddProfile<UserProfile>();
});
var items = new List<User> { new User { Name = "First name" } };
var users = new PaginatedList<User>(items, 1, 0, 1);
var usersDtos = Mapper.Map<PaginatedList<UserDto>>(users);
}
}
add a comment |
Automapper is perfect for me, but are going to need to create and default constructor on the PaginatedList
class
public PaginatedList()
{
PageIndex = 0;
TotalRecords = 0;
TotalPages = 0;
}
-------------------------------- Example
class User
{
public string Name { get; set; }
}
class UserDto
{
public string NameDto { get; set; }
}
public class UserProfile: Profile
{
public UserProfile()
{
CreateMap<User, UserDto>()
.ForMember(target => target.NameDto, x => x.MapFrom(source => source.Name))
.ReverseMap();
}
}
internal class Program
{
private static void Main(string args)
{
Mapper.Initialize(config =>
{
config.AddProfile<UserProfile>();
});
var items = new List<User> { new User { Name = "First name" } };
var users = new PaginatedList<User>(items, 1, 0, 1);
var usersDtos = Mapper.Map<PaginatedList<UserDto>>(users);
}
}
Automapper is perfect for me, but are going to need to create and default constructor on the PaginatedList
class
public PaginatedList()
{
PageIndex = 0;
TotalRecords = 0;
TotalPages = 0;
}
-------------------------------- Example
class User
{
public string Name { get; set; }
}
class UserDto
{
public string NameDto { get; set; }
}
public class UserProfile: Profile
{
public UserProfile()
{
CreateMap<User, UserDto>()
.ForMember(target => target.NameDto, x => x.MapFrom(source => source.Name))
.ReverseMap();
}
}
internal class Program
{
private static void Main(string args)
{
Mapper.Initialize(config =>
{
config.AddProfile<UserProfile>();
});
var items = new List<User> { new User { Name = "First name" } };
var users = new PaginatedList<User>(items, 1, 0, 1);
var usersDtos = Mapper.Map<PaginatedList<UserDto>>(users);
}
}
answered Jan 3 at 3:12
Fredy Adriano Jimenez MartinezFredy Adriano Jimenez Martinez
30427
30427
add a comment |
add a comment |
I ended up creating another factory method within the PaginatedList which will handle the conversion.
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
Ensure.It.IsGreaterThan(0, pageIndex);
Ensure.It.IsGreaterThan(0, pageSize);
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
Then make the existing CreateAsync
method to call this new CreateAsync<TSource>
method.
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
With this, if we want to keep returning the same Entity class, we can use it like this
await PaginatedList<User>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
And if we want to convert the entity class to return a Dto class or other class, it can be used like this
await PaginatedList<UserDto>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
If we don't convert the Entity into other class, we don't need to specify anything within the automapper configuration. But if you want to map the entity into other class, then it needs to be configured within the automapper.
More or less, here is how the PaginatedList class looks like
public class PaginatedList<TResult> : List<TResult>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<TResult> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = count;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
}
add a comment |
I ended up creating another factory method within the PaginatedList which will handle the conversion.
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
Ensure.It.IsGreaterThan(0, pageIndex);
Ensure.It.IsGreaterThan(0, pageSize);
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
Then make the existing CreateAsync
method to call this new CreateAsync<TSource>
method.
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
With this, if we want to keep returning the same Entity class, we can use it like this
await PaginatedList<User>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
And if we want to convert the entity class to return a Dto class or other class, it can be used like this
await PaginatedList<UserDto>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
If we don't convert the Entity into other class, we don't need to specify anything within the automapper configuration. But if you want to map the entity into other class, then it needs to be configured within the automapper.
More or less, here is how the PaginatedList class looks like
public class PaginatedList<TResult> : List<TResult>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<TResult> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = count;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
}
add a comment |
I ended up creating another factory method within the PaginatedList which will handle the conversion.
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
Ensure.It.IsGreaterThan(0, pageIndex);
Ensure.It.IsGreaterThan(0, pageSize);
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
Then make the existing CreateAsync
method to call this new CreateAsync<TSource>
method.
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
With this, if we want to keep returning the same Entity class, we can use it like this
await PaginatedList<User>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
And if we want to convert the entity class to return a Dto class or other class, it can be used like this
await PaginatedList<UserDto>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
If we don't convert the Entity into other class, we don't need to specify anything within the automapper configuration. But if you want to map the entity into other class, then it needs to be configured within the automapper.
More or less, here is how the PaginatedList class looks like
public class PaginatedList<TResult> : List<TResult>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<TResult> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = count;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
}
I ended up creating another factory method within the PaginatedList which will handle the conversion.
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
Ensure.It.IsGreaterThan(0, pageIndex);
Ensure.It.IsGreaterThan(0, pageSize);
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
Then make the existing CreateAsync
method to call this new CreateAsync<TSource>
method.
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
With this, if we want to keep returning the same Entity class, we can use it like this
await PaginatedList<User>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
And if we want to convert the entity class to return a Dto class or other class, it can be used like this
await PaginatedList<UserDto>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)
If we don't convert the Entity into other class, we don't need to specify anything within the automapper configuration. But if you want to map the entity into other class, then it needs to be configured within the automapper.
More or less, here is how the PaginatedList class looks like
public class PaginatedList<TResult> : List<TResult>
{
public int PageIndex { get; }
public int TotalRecords { get; }
public int TotalPages { get; }
public PaginatedList(IEnumerable<TResult> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalRecords = count;
TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);
AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
IQueryable<TSource> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(ufc => Mapper.Map<TResult>(ufc))
.ToListAsync();
return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}
public static async Task<PaginatedList<TResult>> CreateAsync(
IQueryable<TResult> source, int pageIndex, int pageSize)
{
return await CreateAsync<TResult>(source, pageIndex, pageSize);
}
}
answered Jan 3 at 23:08
muhihsanmuhihsan
1,16851429
1,16851429
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015297%2fmap-entity-pagination-class-to-dto-pagination-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Deriving from
List<T>
is an extremely poor choice. I highly recommend reading Is-A Versus Has-A.– Erik Philips
Jan 3 at 3:16
@ErikPhilips thanks for the suggestion, I'll take a look
– muhihsan
Jan 3 at 5:55