Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I work on blazor web application with .Net core 7. i face issue when implement service for get all items using pagination .

so on service my issue is can't return ListQueryResult when apply pagination for get all items .

my issue exactly on the following line of code on service

IQueryable<trecord> query = _context.ServerNames;

What I have tried:

pagination modules

 public  record ListQueryRequest
    {
        public int StartIndex { get; init; } = 0;
        public int PageSize { get; init; } = 1000;  //set at some maximum epected count for no paging
        public CancellationToken Cancellation { get; set; } = new();
    }
  public  record ListQueryResult<TRecord>
    {
        public IEnumerable<TRecord> Items { get; init; } = Enumerable.Empty<TRecord>();
        public bool Successful { get; init; }
        public string Message { get; init; } = string.Empty;
        public int TotalCount { get; init; }

        private ListQueryResult() { }

        public static ListQueryResult<TRecord> Success(IEnumerable<TRecord> Items, int totalCount, string? message = null)
            => new ListQueryResult<TRecord> { Successful = true, Items = Items, TotalCount = totalCount, Message = message ?? string.Empty };

        public static ListQueryResult<TRecord> Failure(string message)
            => new ListQueryResult<TRecord> { Message = message };
    }
on generic repository i implement as below :

public interface IRepository<TEntity> where TEntity : class
{
public ListQueryResult<TEntity> GetItems(ListQueryRequest request);
}
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
    internal AppsRepositoryDBContext _context;
    internal DbSet<TEntity> dbSet;

    public BaseRepository(AppsRepositoryDBContext context)
    {
        _context = context;
        this.dbSet = _context.Set<TEntity>();
    }

    public ListQueryResult<TEntity> GetItems(ListQueryRequest request)
    {
        _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
        IQueryable<TEntity> query = _context.Set<TEntity>();
        var count = query.CountAsync(request.Cancellation);
        query = query
            .Skip(request.StartIndex)
            .Take(request.PageSize);
        var list = query.ToList();
        return ListQueryResult<TEntity>.Success(list,Convert.ToInt32(count));
    }

}
here is issue implementing service

public interface IserverNamesService : IRepository<ServerNames>
    {

        ListQueryResult<ServerNames> GetItems(ListQueryRequest request);
    }
Block of code below Have issue when return ListQueryResult on first line .

    public ListQueryResult<ServerNames> GetItems(ListQueryRequest request)
    {
    // here i face issue implementing  service
        IQueryable<TRecord> query = _context.ServerNames;
        query = query
            .Skip(request.StartIndex)
            .Take(request.PageSize);

        query = query
               .Join(_context.ServerTypes,
               sn => sn.ServerTypeId,
               st => st.ServerTypeId,
               (sn, st) => new ServerNames
               {
                   ServerID = sn.ServerID,
                   server_Name = sn.server_Name,
                   ServerType = st.ServerType
               });

    
        var list = query.ToList();
        return list;
       
    }
Posted
Updated 19-Feb-23 23:15pm
Comments
Graeme_Grant 18-Feb-23 20:51pm    
Is this Blzor serverside?
ahmed_sa 19-Feb-23 0:26am    
yes
Graeme_Grant 20-Feb-23 15:57pm    
I did not see your comment as you did not reply to my post.
[no name] 19-Feb-23 11:38am    
Why are you trashing an enumerable with another enumerable? (query = query ...). Use var query because you could also be typing (casting) wrong.
Dave Kreskowiak 20-Feb-23 9:14am    
It's not trashing the previous. That code is getting the query for a list of ServerNames, then appending the .Skip and .Take, and again appending the .Join to the query.

1 solution

Quote:
C#
IQueryable<TRecord> query = _context.ServerNames;
The TRecord generic type parameter is most like not in scope on that line.

And even if it is, you'd be trying to assign a set of ServerNames objects to a set of TRecord instances. Unless you have a generic type constraint in place to ensure that TRecord is derived from ServerNames, that's not going to work.

Instead, you need to use the correct type:
C#
IQueryable<ServerNames> query = _context.ServerNames;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900