1

I use Servietack and OrmLite in our project. When the request (Db.Select) is sent, after much time, no response return.

       public object Get(GetCategoryNews request)
        {
            var PageCount = 20;
            var skip = (request.Page.GetValueOrDefault(1) - 1) * PageCount;

            var cacheKey = UrnId.Create<GetCategoryNews>(DateTime.Now.ToString("yyMMddhhmm") + skip);
            return base.Request.ToOptimizedResultUsingCache(base.Cache, cacheKey, () =>
            {
            //On this line, waiting and no response
              var status = Db.Select<News>().Skip(skip).Take(PageCount).Select(o => new NewsResponse() {
                    Id = o.Id,
                    CreateOn = o.CreateOn,
                    Description = o.Description,
                    PublishOn = o.PubDate,
                    Title = o.Title
                });
                return status;
            });
        }

but when Count request, It work well

    var count = Db.Count<News>();

How do I fix this?

Gray
  • 115,027
  • 24
  • 293
  • 354
AmirHossein
  • 367
  • 1
  • 8
  • 25

1 Answers1

2

Calling Db.Select<News>() returns a List<News> which downloads your entire table results into an in memory list collection. Since you're trying to page the resultset you need to instead build an SqlExpression that OrmLite executes to only return the results you're interested in, e.g:

//Create SQL Query
var q = db.From<News>()
          .Skip(skip)
          .Take(PageCount);

var results = db.Select(q); //Executes paged query
return results.Select(o => new NewsResponse {
    Id = o.Id,
    CreateOn = o.CreateOn,
    Description = o.Description,
    PublishOn = o.PubDate,
    Title = o.Title
});
mythz
  • 141,670
  • 29
  • 246
  • 390
  • thanks guy, How can I query in nested objects, for example = var q = Db.From() .Where(o => o.Feed.Category.CategoryId == 10); – AmirHossein Apr 22 '17 at 18:44
  • @AmirHossein Please don't put code in comments, can you ask a new question. – mythz Apr 22 '17 at 18:47
  • @AmirHossein That's likely not the answer you want, you can use [Typed Where Expression](https://github.com/ServiceStack/ServiceStack.OrmLite#convenient-common-usage-data-access-patterns). – mythz Apr 22 '17 at 19:05