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?

1 Answer 1

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
});
Sign up to request clarification or add additional context in comments.

3 Comments

thanks guy, How can I query in nested objects, for example = var q = Db.From<News>() .Where(o => o.Feed.Category.CategoryId == 10);
@AmirHossein Please don't put code in comments, can you ask a new question.
@AmirHossein That's likely not the answer you want, you can use Typed Where Expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.