开发者

Does it make sense to return PageList<T> from my Model if I'm not using LINQ's lazy loading?

开发者 https://www.devze.com 2022-12-14 07:14 出处:网络
I\'m working on a messaging system for a site of mine written in ASP.NET MVC.Right now, I\'m attempting to iron out Pagination for the Inbox/Sent views.

I'm working on a messaging system for a site of mine written in ASP.NET MVC. Right now, I'm attempting to iron out Pagination for the Inbox/Sent views.

I've stumbled onto Rob Connery's PagedList<T>, which seems like a nice solution to the problem.

However, one difference between my use-case and Rob's however is that I don't return IQueryable from my Model, and all queries I run with .ToList() after them (then returning an IEnumerable), to ensure that my query is executed when the Model's method is called.

开发者_如何学GoBecause of this difference, does it still make sense to use his PagedList<T>? I basically just want to return a subset of the total results, along with some other properties like "HasNextPage", "Total Count", etc.

However, because I'm not letting the PagedList class call an IQueryable directly, I'm wondering if I should just create my own basic class with those properties that I want?

I know how to take a particular page of results using LINQ:

public IEnumerable<Entities.Message> GetInboxMessages(int userID, int pageSize, int pageIndex)
{
     using (MainDataContext db = new MainDataContext())
     {
         var messages = (from m in db.Messages
                         join o in db.MessageRecipients on m.MessageID equals o.MessageID
                         orderby m.DateSent
                         select new Entities.Message
                         {
                            // ... 

                          }).Skip(pageIndex * pageSize).Take(pageSize).ToList();

          return messages;
      }
  }

But without the other information I need about HavingNextPage, TotalItemsCount, etc.

What should I do in this case? Can I still use the above PagedList<T>, even though I don't plan on doing Lazy Loading? Should I just create my own?


You can still use PagedList. The IQueryable support is just one part, all of the other bits will work fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号